Dear Arduino Community,
I would like to store a int32_t variable into two int16_t variables. After storing i would like to rebuild this int32_t variable from its int16_t components.
I started to write a code based on this page:
But my code does not give back the original value which I would like, and I don't see where I was wrong.
Could some one point me out the right direction? Any help appreciated!
const byte lngth=8;
int32_t timeSeries[lngth];
byte byteArray[4];
int16_t intArray1[2];
int16_t intArray2[2];
int16_t intArray3[2];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
delay(1000);
for (int i=0; i < sizeof(timeSeries); i++) {
timeSeries[i]=0;
}
}
void loop() {
// Splitting int32_t variable to 4 int8_t variables
byteArray[0] = (byte) ((timeSeries[0] >> 24) & 0xFF) ;
byteArray[1] = (byte) ((timeSeries[0] >> 16) & 0xFF) ;
byteArray[2] = (byte) ((timeSeries[0] >> 8) & 0xFF) ;
byteArray[3] = (byte) ((timeSeries[0]) & 0xFF) ;
// Splitting int32_t variable to 2 int16_t variables
intArray2[0] = (int16_t) ((timeSeries[0] >> 16) & 0xFFFF) ;
intArray2[1] = (int16_t) ((timeSeries[0]) & 0xFFFF) ;
// Splitting int32_t variable to 2 uint16_t variables
intArray3[0] = (uint16_t) ((timeSeries[0] >> 16) & 0xFFFF) ;
intArray3[1] = (uint16_t) ((timeSeries[0]) & 0xFFFF) ;
// Combining together the parts
timeSeries[1] = ( (byteArray[0] << 24) + (byteArray[1] << 16) + (byteArray[2] << 8) + (byteArray[3] ) ); // Method 1
intArray1[0] = ( (byteArray[0] << 8) + (byteArray[1] ) ); // Method 1
intArray1[1] = ( (byteArray[2] << 8) + (byteArray[3] ) ); // Method 1
timeSeries[2] = ( (intArray1[0] << 16) + (intArray1[1]) ); // Method 1
timeSeries[3] = ( (int32_t) intArray1[0] ) << 16 | intArray1[1] ; // Method 2
timeSeries[4] = ( (intArray2[0] << 16) + (intArray2[1]) ); // Method 1
timeSeries[5] = ( (int32_t) intArray2[0] ) << 16 | intArray2[1] ; // Method 2
timeSeries[6] = ( (intArray3[0] << 16) + (intArray3[1]) ); // Method 1
timeSeries[7] = ( (int32_t) intArray3[0] ) << 16 | intArray3[1] ; // Method 2
Serial.print(timeSeries[0]);
for (int i=1; i < lngth; i++) {
Serial.print(';');
Serial.print(timeSeries[i]);
}
Serial.println();
delay(1000);
timeSeries[0] = timeSeries[0] + 30000;
}