Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access binary data within a string in javascript

I use a device driver that captures the input from a reader (RFID) and sends it to the keyboard buffer (keyboard wedge). The captured data can (and must) be transformed intermediately with java script.

This javascript is processed within the driver context - unfortunately the javascript gets the captured "binary" data in a DATA variable of type string.

You can imagine what javascript does: it interprets the input as unicode and thus does not let you address byte by byte within the string - it changes arbitrarily between 1 ...4 bytes length depending on the value.

I simply need to get the binary string transformed to its readable string format: xf9268970 should read "f9268970". Whatever I tried sucked so far.

Thanks for any tip!

like image 966
Klaus Avatar asked Mar 23 '26 02:03

Klaus


1 Answers

First, a disclaimer. I haven't worked with binary data and javascript but maybe this could help.

Maybe you could loop through the string and use charAt to look at each character. From what I understand charAt returns an ASCII value (not unicode). So instead of 4 bytes you would get 2 bytes(?)

var character;
for (var i = 0; i < str.length; i++) {
    character = str.charAt(i);
}

Maybe this reference will point you in the right direction: charAt reference

like image 87
Helgi Avatar answered Mar 25 '26 17:03

Helgi



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!