简体   繁体   English

十六进制转 int32 大端

[英]Hex to int32 Big Endian

I am trying to convert this hex to the correct INT32 Big Endian that would be:我正在尝试将此十六进制转换为正确的 INT32 Big Endian,即:

ffd7c477 --> -2636681

I checked how it should look here:我在这里检查了它的外观:

http://www.scadacore.com/tools/programming-calculators/online-hex-converter/ http://www.scadacore.com/tools/programming-calculators/online-hex-converter/

I dont know how to convert it.我不知道如何转换它。 This is where the latitude is这是纬度所在

payload = "1901000a03010aff01ff01300a01ffd7c4750016c0540322ed"
latitude = payload[28:36] = ffd7c477 

Here I get the wrong unsigned value:在这里我得到错误的无符号值:

int(binary[28:36], 16)

这有效struct.unpack('>i', "ffd7c477".decode('hex'))

Since Python will use the byteorder of your processor architecture by default to handle numbers (you can check your systems byteorder with sys.byteorder ), you'll have to explicitly specify that you want to treat the given value as big endian.由于 Python 将默认使用处理器架构的字节顺序来处理数字(您可以使用sys.byteorder检查您的系统字节sys.byteorder ),因此您必须明确指定要将给定值视为大端。 The struct module will allow you to do this: struct模块将允许您执行此操作:

import struct, codecs
val = "ffd7c477"
struct.unpack("!i", codecs.decode(val, "hex"))

The first argument of unpack : ! unpack的第一个参数: ! means to treat the bytes as big endian, i means to treat the bytes as int32 values.意味着将字节视为大端, i意思是将字节视为 int32 值。

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

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