3

I want to ask I want to convert my binary data to hex before I will insert this to my table.

var net  = require('net');

var server = net.createServer(function(socket){

    socket.on('data',function(data){
    var bindata= data.toString('binary');

    //filter(bindata);

    //if no error convert to hex.
    var hexdata = bindata.toString('hex');

    //insert hexdata here.
});

server.listen(3030,'127.0.0.1', function () {
    console.log("server is listenining");
});

but the problem is that the binary data will be inserted.

2 Answers 2

13
parseInt("10101001", 2).toString(16)
// => "a9"

EDIT: I think I misunderstood the question. Your data starts out as Buffer, then you convert it to a string, then you want it as hex? If so, just do data.toString('hex'). If you have already manipulated bindata, then reconstruct into a buffer:

var bindata = data.toString('binary');
// do something with bindata
var hexdata = new Buffer(bindata, 'ascii').toString('hex');
Sign up to request clarification or add additional context in comments.

8 Comments

Why ascii? Because that is what you would get in your original data buffer if someone was sending application/x-octet-stream-compatible content. If they were sending UTC strings, then I assume you wouldn't be messing with hex in the first place.
can I ask why is it after I converted into image file,the image is not properly converted enventhough I get the complete binary image data.there is stripe lines along the image.
With no access to your code or your image file, no idea. diff it. Use vbindiff or bsdiff or similar to find what is actually different.
I already ask here stackoverflow.com/questions/30272013/… , but no one tries to attempt to help me. I hope you can help me.Thank you in advance.
I will try to put in pastie all the binary image data that was save in my db.I'll be back
|
0

Try this:

parseInt("1111", 2).toString(16);

Second param in parseInt is radix its represent "111" value to binary and toString(16) convert it to hex.

1 Comment

This appears to be the same as the existing answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.