5

I am trying to convert an ASCII string into a byte array.

Problem is my code is converting from ASCII to a string array and not a Byte array:

var tx = '[86400:?]';
for (a = 0; a < tx.length; a = a + 1) {
 hex.push('0x'+tx.charCodeAt(a).toString(16));
}

This results in:

 [ '0x5b','0x38','0x36','0x30','0x30','0x30','0x3a','0x3f','0x5d' ]

But what I am looking for is:

[0x5b,0x38 ,0x30 ,0x30 ,0x30 ,0x30 ,0x3a ,0x3f,0x5d]

How can I convert to a byte rather than a byte string ?

This array is being streamed to a USB device:

device.write([0x5b,0x38 ,0x30 ,0x30 ,0x30 ,0x30 ,0x3a ,0x3f,0x5d])

And it has to be sent as one array and not looping sending device.write() for each value in the array.

Cossintan
1143 silver badges12 bronze badges
asked Jun 3, 2013 at 9:38
3
  • 4
    0x5b isn't actually 0x5b, but rather it's just a simple int with the value 91. Save these as an int (tx.charCodeAt(a)) instead, and everything will be fine. Commented Jun 3, 2013 at 9:42
  • 1
    Numbers are numbers, and they're always stored in binary. If you want the hex representation, store it as a string (as you are now), or use hex.push(tx.charCodeAt(a));, which will store the ASCII codes, and use .toString(16) to convert to hex while printing. Commented Jun 3, 2013 at 9:44
  • possible duplicate of JavaScript Converting string values to hex Commented Nov 19, 2013 at 8:30

2 Answers 2

18

A single liner :

 '[86400:?]'.split ('').map (function (c) { return c.charCodeAt (0); })

returns

 [91, 56, 54, 52, 48, 48, 58, 63, 93]

This is, of course, is an array of numbers, not strictly a "byte array". Did you really mean a "byte array"?

Split the string into individual characters then map each character to its numeric code.

Per your added information about device.write I found this :

Writing to a device

Writing to a device is performed using the write call in a device handle. All writing is synchronous.

device.write([0x00, 0x01, 0x01, 0x05, 0xff, 0xff]);

on https://npmjs.org/package/node-hid

Assuming this is what you are using then my array above would work perfectly well :

device.write('[86400:?]'.split ('').map (function (c) { return c.charCodeAt (0); }));

As has been noted the 0x notation is just that, a notation. Whether you specify 0x0a or 10 or 012 (in octal) the value is the same.

answered Jun 3, 2013 at 9:50
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, yes it has to be a Hex byte array as the output is streamed to a usb device: device.write([0x5b,0x38 ,0x30 ,0x30 ,0x30 ,0x30 ,0x3a ,0x3f,0x5d]). So I really need a way of creating a hex-byte array !
a "byte array" is not "hex", it is often merely presented in hex.
What environment are you running? node.js ?
Excellent, thanks, yes it is node.js and your googling was correct this is using node-hid. After some more testing, I now understand that it's not necessary to send the array in hex format so your solution works. Thanks again.
2
 function getBytes(str){
 let intArray=str.split ('').map (function (c) { return c.charCodeAt (0); });
 let byteArray=new Uint8Array(intArray.length);
 for (let i=0;i<intArray.length;i++)
 byteArray[i]=intArray[i];
 return byteArray;
 }
 device.write(getBytes('[86400:?]'));
answered Dec 4, 2020 at 10:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.