This will take a buffer hex and convert it to a binary str and back to the buffer hex.
NOTE: when I say a buffer hex, I mean a decimal value because when you iterate over a buffer and pull each item in the array, it gives you the decimal value (eg: 210, instead of d2).
var buffer - new Buffer([0, 210, 242]); // Node
// var arrayBuffer = new ArrayBuffer(3); // JavaScript
// var uint8 = new Uint8Array(arrayBuffer); // JavaScript/ 16Array, 32Array, etc
Need to be acquainted with buffers
You'll iterate over the buffer with a for(){}
and inside you can do something like:
(210).toString(2); // '11010010'
(210).toString(16); // 'd2' (untested)
(210).toString(8); // (Octal-Digits representation)
parseInt((210).toString(2), 2); // 210
parseInt((210).toString(2), 2).toString(16); // 'd2'
Obviously, instead of using "(210).toString(2)
" IN YOU FOR LOOP, you would use "(buffer[i]).toString(2)
"
Endian Rep is up to you! :) (array.reverse())
Hope this helps!
PS. parseInt(('00000' + (210).toString(2).substring(5, 8)), 2); // 2
parseInt((210).toString(2).substring(5, 8), 2); // 2