Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert binary to hex in node.js

Tags:

node.js

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.

like image 326
jemz Avatar asked Feb 12 '26 01:02

jemz


1 Answers

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');
like image 167
Amadan Avatar answered Feb 14 '26 17:02

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!