Hello I'm newbie with arduino and maybe is a silly question, but I'm trying to convert long
numbers to byte
.
My code is:
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]);
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?
The second problem is that payload[0]
should be 6A
hex, but my println
shows 106
. Why is not converting it correctly? Does this problem comes because of the previous error?
Below I show a scheme of what I'm doing. It's from the this link.
Thank you very much!
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?
This is probably due to precision limits of float
. Try double
instead.
The second problem is that payload[0] should be 6A hex, but my println shows 106.
You can provide a format for println(), like this:
SerialUSB.println(payload[0], HEX);
User contributions licensed under CC BY-SA 3.0