简体   繁体   English

将十六进制字符串数组转换为数字

[英]Converting an Array of Hexadecimal Strings to Numbers

I have an application in which a server is sending a lot of data every second to clients that request it. 我有一个应用程序,其中服务器每秒向请求它的客户端发送大量数据。 In this data set are several (large) arrays that contain numbers (in some cases 16-bit integers and in others double precision floats). 在此数据集中,有几个包含数字的(大)数组(在某些情况下为16位整数,在其他情况下为双精度浮点数)。 In order to reduce the packet size, I would like to send only the number of bytes that are necessary to represent the number (2 bytes for the 16 bit and 8 for the 64 bit). 为了减小数据包的大小,我只想发送表示该数字所必需的字节数(16位为2个字节,而64位为8个字节)。 I know that I can do something like this: 我知道我可以做这样的事情:

/* Create space for 4 32 bit numbers */
var buffer = new ArrayBuffer(16);

/* Hex representation of [0, 123, 246, 369] */
buffer = ["0x0", 0x7B", "0xF6", "0x171"];

/* Actual array containing numbers above */
var int32View = new Int32Array(buffer); 

but....I don't want to prepend everything sting with "0x" (adding two bytes will double the size of the 16 bit numbers). 但是....我不想在所有字符串前面加上“ 0x”(增加两个字节将使16位数字的大小加倍)。 I think I might be missing something - is there a better way to do this such that I don't need to indicate that the strings are hexadecimal representations of numbers (ie can I drop the "0x")? 我想我可能会缺少一些东西-有更好的方法做到这一点,这样我就不必指出字符串是数字的十六进制表示(即我可以删除“ 0x”)吗?

Thanks, Matt 谢谢,马特

You can use map() or a for-loop to treat each value and use the length as basis for where to sort them out. 您可以使用map()或for循环来处理每个值,并使用长度作为排序依据。

One note though, you mention 64-bit floats. 需要注意的是,您提到的是64位浮点数。 When these are extracted from a buffer they will be 64-bit integer values (ie. 0xXXXXXXXXXXXXXXXX) formatted in IEEE-754 format. 从缓冲区中提取这些值时,它们将是以IEEE-754格式格式化的64位整数值(即0xXXXXXXXXXXXXXXXX)。

However, JavaScript can only work with 32-bit values (0xXXXXXXXX) so you have to split the processing of those into two parts using a 32-bit unsigned array, then add a Float64Array view for that later. 但是,JavaScript只能使用32位值(0xXXXXXXXX),因此您必须使用32位无符号数组将它们的处理分为两部分,然后为以后添加Float64Array视图。

If the float values are literals, ie "2.345", "-1.33" etc. then you can simply use parseFloat(str) on them (instead of handling 64-bit values as below). 如果浮点值是文字,即“ 2.345”,“-1.33”等,则可以对它们简单地使用parseFloat(str) (而不是像下面那样处理64位值)。

Example

 // inserted 64-bit IEEE float of -2.3357 = 0x7b4a233ac002af83 var normArray = ["0", "7B", "F6", "7b4a233ac002af83", "171"], int16a = [], int32a = [], int16b, int32b, float64b; normArray.map(function(entry) { if (entry.length > 4) { var high = entry.substr(0, 8), low = entry.substr(8, 8); int32a.push(parseInt(high, 16), parseInt(low, 16)); } else { int16a.push(parseInt(entry, 16)); } }); // convert to typed arrays int16b = new Int16Array(int16a); int32b = new Uint32Array(int32a); float64b = new Float64Array(int32b.buffer); // convert IEEE rep. to floats document.writeln("<pre>int16 : " + int16b[0] + ", " + int16b[1] + ", " + int16b[2] + ", ..."); document.write("float64: " + float64b[0] + "</pre>") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM