//FUNCTION DECLARATION
void int2bytes(int, char[]);
float int2float(int, float);
int bytes2int(char[]);
float bytes2float(char[]);
void setup()
{
Serial.begin(19200);
}
void loop()
{
//test integers
int i1 = 13106; //HEX 33:32
int i2 = 13181; //HEX 33:7d
//blank char arrays
char result1[2] = {};
char result2[2] = {};
//convert integers into chars
int2bytes(i1, result1);
int2bytes(i2, result2);
Serial.println(result1[1],HEX);
Serial.println(result1[2],HEX);
delay(2000);
Serial.println(result2[1],HEX);
Serial.println(result2[2],HEX);
delay(1000);
Serial.println(bytes2int(result1)); //convert back to int
Serial.println(bytes2int(result2)); //convert back to int
delay(1000000); //stop from repeating
}
void int2bytes(int integer, char bytes[])
{
char high = (integer >> 8) & 0xFF;
char low = integer & 0xFF;
//Serial.println(high,HEX);
//Serial.println(low,HEX);
delay(1000);
bytes[1] = high;
bytes[2] = low;
}
/*
This function will take 2 bytes in an array (little endian format) and convert them into a integer which will be returned.
*/
int bytes2int(char bytes[])
{
int i;
memcpy(&i, bytes, sizeof(int));
return i;
}
/*
This function will take 4 bytes in an array (little endian format) and convert them into a float which will be returned.
*/
float bytes2float(char bytes[])
{
float f;
memcpy (&f, bytes, sizeof(float));
return f;
}
And here is what I am getting as a result, as you can see the integers are not the same as original input
These methods are not suppose to be used together. I am using them over a serial protocol, I just thought I should not lose information going back and forth. I do not understand fully bitwise shifting, so I am a little weary to use it. The memcpy makes more sense to me, but I don't know how to use it to split up bytes. Hopefully that answers your question.
The values you are getting back should give you a clue:
13056 = 0x3300
13106 = 0x3332
I'm not sure where the 00 is coming from, but there is one little magic word you need to know and understand: ENDIAN
The endian of a system defines which byte comes first in a word, the most or least significant. "Big Endian" is where the most significant byte comes first. This is when 13106 contains the bytes 0x33 and 0x32. With "Little Endian" the bytes are the other way around, so 13106 contains the bytes 0x32 and 0x33.
Now, the Arduino is little endian. Your bit shifting very nicely stores the bytes of the word in an array in big-endian mode. Your memcpy then blindly dumps those in that same order into an integer - so you have effectively reversed the bytes.
Thank you, I have looked up endian, and the other ones don't "blindly" put them together. They are suppose to operate with little endian. I thank you for pointing out the 00. I tried to reverse high and low, in my function int2bytes, but that did not work.
Thank you for the 00 comment. I figured out the problem.I just realized yesterday I was using MATLAB so I was not using zero based arrays. Here is the corrected code, if you guys notice any problems with this code as well let me know.
//FUNCTION DECLARATION
void int2bytes(int, char[]);
int bytes2int(char[]);
float bytes2float(char[]);
void setup()
{
Serial.begin(19200);
}
void loop()
{
//test integers
int i1 = 13106; //HEX 33:32
int i2 = 13181; //HEX 33:7d
//blank char arrays
char result1[2] = {};
char result2[2] = {};
//convert integers into chars
int2bytes(i1, result1);
int2bytes(i2, result2);
Serial.println(result1[0],HEX);
Serial.println(result1[1],HEX);
delay(2000);
Serial.println(result2[0],HEX);
Serial.println(result2[1],HEX);
delay(1000);
Serial.println(bytes2int(result1)); //convert back to int
Serial.println(bytes2int(result2)); //convert back to int
delay(1000000); //stop from repeating
}
void int2bytes(int integer, char bytes[])
{
char high = (integer >> 8) & 0xFF;
char low = integer & 0xFF;
//Serial.println(high,HEX);
//Serial.println(low,HEX);
delay(1000);
bytes[0] = low;
bytes[1] = high;
}
/*
This function will take 2 bytes in an array (little endian format) and convert them into a integer which will be returned.
*/
int bytes2int(char bytes[])
{
int i;
memcpy(&i, bytes, sizeof(int));
return i;
}
/*
This function will take 4 bytes in an array (little endian format) and convert them into a float which will be returned.
*/
float bytes2float(char bytes[])
{
float f;
memcpy (&f, bytes, sizeof(float));
return f;
}
You should either use bitshifting for both operations, or use memcpy() for both operations. Don't mix the two together, or problems like this can creep in.