I'm trying to convert an integer into a byte array and back again.
The maximum value of my integer need only be 255,255 (2 bytes).
I really appreciate the help.
I'm trying to convert an integer into a byte array and back again.
The maximum value of my integer need only be 255,255 (2 bytes).
I really appreciate the help.
If you're just going to use the bytes in an int I would do:
byte low = e;
byte high = e >> 8;
But I think a
typedef struct twobytes {
byte low;
byte high;
} uint2x8;
uint82x8 myTwoBytes = { 10 , 255 };
myTwoBytes.low = 60;
myTwoBytes.high = 24;
or
byte myTwoBytes[2] = { 10 , 255 };
myTwoBytes[0] = 60;
myTwoBytes[1] = 24;
solution will be more intuitive to program.
Ah, I wasn't clear. I'm not trying to send two different integers each as a separate byte, but one big integer as separate bytes.
If i was sending a number from 0 to 255 it would be easy, I would just send 0x00 - 0xFF, but what if I want to send a number longer than one byte, say 23,582?
How would I convert 23,582 into bytes that I could send one at a time.
On the receiving end how could I turn these bytes back into an integer?
I hope I'm making sense.
Thanks
Arduino now has functions to do that for you.
word(high, low);
returns the integer value of the high and low bytes
highByte(int);
lowByte(int);
return the high and low bytes from an integer
The following is an example of how these are used:
byte myTwoBytes[2] = { 10 , 255 };
int val = word( myTwoBytes[0], myTwoBytes[1]);
Serial.println(val);
Serial.print( highByte(val));
Serial.print( ",");
Serial.println(lowByte(val));
How best to send the data depends on what is receiving it, you may want to say what that is.
I'm really trying to understand. Here is a simplified version of what I'm trying to do.
int myDelay = 23000 // this is for milliseconds
/* this is a packet sent over the UART (Zigbee)
see the 0x??, 0x?? .. those are the bytes i have reserved in the packet to send my integer.*/
int packet[]={0x7E,0x00,0x06,0x00,0x00,0xFF,0x00,0x??,0x??,0x4D};
I'm still at a loss of how to turn 23000 into two bytes (Hex).
My apologies if I'm n00bing it up, I'm pretty new to this stuff.
I'm still at a loss of how to turn 23000 into two bytes (Hex).
does the following do what you want?
Serial.print( highByte(23000),HEX);
Serial.print(lowByte(23000),HEX);
Not sure what you want to do with the packet array, perhaps you can clarify.
I'm trying to convert a human friendly value into series of bytes to be sent as a serial command packet.
will this work? ..where i have high and low in my byte array? ...
int myDelay = 23000 // this is for milliseconds
byte high = highByte(myDelay);
byte low = lowByte(myDelay);
int packet[]={0x7E,0x00,0x06,0x00,0x00,0xFF,0x00,high,low,0x4D};
Do I have to format the high and low bytes into Hex values .. like the rest of the data in the packet?
You don't need to convert them into hex, that should work.
packet is an array of bytes so you save some ram if you do the following:
byte packet[]={0x7E,0x00,0x06,0x00,0x00,0xFF,0x00,high,low,0x4D};
Thanks! Its clear now & my code is working :).