错误的有效负载转换 Arduino

Wrong payload conversion Arduino

你好,我是 arduino 的新手,这可能是一个愚蠢的问题,但我正在尝试将 long 数字转换为 byte

我的代码是:

float f_longitud = 179.1234567;
byte payload[4];

long longitud= f_longitud * 10000000;
SerialUSB.println(longitud);

payload[0] = (byte) ((longitud & 0xFF000000) >> 24 );
payload[1] = (byte) ((longitud & 0x00FF0000) >> 16 );
payload[2] = (byte) ((longitud & 0x0000FF00) >> 8 );
payload[3] = (byte) ((longitud & 0X000000FF));
SerialUSB.println(payload[0]);

问题是第一个println理论上必须显示1791234567,因为我只是乘以179.1234567 x 10000000但它显示1791234560。为什么会出现这个 0? 7 在哪里?

第二个问题是payload[0]应该是6A十六进制,但是我的println显示106。为什么转换不正确?这个问题是因为之前的错误吗?

下面我展示了我正在做的事情的方案。它来自 this link.

非常感谢!

The problem is that the first println theoretically has to show 1791234567, because I'm just multiplying 179.1234567 x 10000000 but it shows 1791234560. Why is this 0 appearing? Where is the 7?

这可能是由于 float 的精度限制。请尝试 double

The second problem is that payload[0] should be 6A hex, but my println shows 106.

您可以为 println() 提供格式,例如:

SerialUSB.println(payload[0], HEX);