Klaus
Klaus

Reputation: 31

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!

Upvotes: 3

Views: 1061

Answers (2)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

I think you should be listening for keystrokes individually and recording the values in a variable instead of reading a text box.

Upvotes: 0

Helgi
Helgi

Reputation: 1577

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

Upvotes: 1

Related Questions