最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript ascii string to hex byte array - Stack Overflow

programmeradmin4浏览0评论

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
  • 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. – h2ooooooo 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. – Osiris Commented Jun 3, 2013 at 9:44
  • possible duplicate of JavaScript Converting string values to hex – e-sushi Commented Nov 19, 2013 at 8:30
Add a comment  | 

2 Answers 2

Reset to default 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.

   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:?]'));
发布评论

评论列表(0)

  1. 暂无评论