开发者

Difficulty coverting unsigned int16 and unsigned int32 in javascript

开发者 https://www.devze.com 2023-03-22 05:13 出处:网络
I\'m working with an api that sends data in a series of base64 strings that I\'m converting into an array of bytes. I\'m been able to parse the time values sent in the data (year, day, hour etc. The a

I'm working with an api that sends data in a series of base64 strings that I'm converting into an array of bytes. I'm been able to parse the time values sent in the data (year, day, hour etc. The api lists their datatype as unsigned char). I'm using parseInt(..., 2) in javascript.

The difficulty I'm having is converting signed int32 and unsigned int16 into their decimal values. For example, these are the bit stings for voltage and power:

Voltage (unsigned int16 ) 01101010 00001001 - Should be around 120.0 Power (signed int32) 10101010 00010110 00000000 00000000 - Should be 0-10 kWh

Does anyone know how I can convert these values? Also, I wrote a simple function to convert base64 to an array of bytes that I'm pret开发者_如何学Pythonty sure is correct, but the above values don't make any sense maybe it isn't. If that's the case, does anyone know of a plugin that converts base64 to binary.

Thanks, Tristan


I can't see how 0110101000001001 converts into 120... It's either 27415 or 2410 depending on endianness


Your voltage as an unsigned int is 27145, is that what you're getting from your conversion, because it is the correct value. Your power is -1441398784 as a signed int.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号