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.
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.
Share Improve this question edited Sep 6, 2018 at 9:12 Cossintan 1143 silver badges12 bronze badges asked Jun 3, 2013 at 9:38 crankshaftcrankshaft 2,6775 gold badges46 silver badges82 bronze badges 3 |2 Answers
Reset to default 18A 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.
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:?]'));
0x5b
isn't actually0x5b
, but rather it's just a simpleint
with the value91
. Save these as an int (tx.charCodeAt(a)
) instead, and everything will be fine. – h2ooooooo Commented Jun 3, 2013 at 9:42hex.push(tx.charCodeAt(a));
, which will store the ASCII codes, and use.toString(16)
to convert to hex while printing. – Osiris Commented Jun 3, 2013 at 9:44