Variables - Convert 16 bit into 2x 8 bit and vice versa

Hi there,

I am having trouble finding how to convert a 16bit variable into two 8 bit variables. This is to store on a EEPROM.

The same is varil to read several 8bit variables and convert them back into 16 and 32bit words.

Thanks in Advance for any help

You can mask off the bytes you want to use, or you can maybe look into a Union.

int Data = 12345;
byte Hi = Data & 0xFF00;
byte Lo = Data & 0x00FF;
. . .
// print bytes
union myData {
  int Data;
  byte Hi;
  byte Lo;
} myBytes;

. . .

myBytes.Data = 12345;
Serial.print(myBytes.Hi);
Serial.print(" ");
Serial.println(myBytes.Lo);

HazardsMind: You can mask off the bytes you want to use, ... [/url].

How would I do this?

Regards

There are also the highByte and lowByte functions. Look in the reference.

I think you missed a shift in your first Hi calculation.

The above is good to write a single variable, but I have up to 32K of memory.

What I need is some function that will take a 16bit variable and output 2 8bit variables, or vice versa. Is there anything similar to this?

**I left out a very important part that allows you to do what you want, and that part is a struct. If you tried my first code, you would have seen that both Hi and Lo output the same thing. Well with a struct, it will now actually split the memory of the int into Hi and Lo.

Use an array

union myData 
{
  int Data;
  struct bytes
  {
    byte Lo; // <- notice the order, Lo is first. The explanation to this is because memory is read as Lo to Hi, not Hi to Lo
    byte Hi;
  }B;
}myBytes[20];

void setup()
{
  Serial.begin(115200);
  for(byte i = 0; i < 20; i++)
    myBytes[i].Data = random(10000,32000);

  for(byte j = 0; j < 20; j++)
  {
    Serial.print(myBytes[j].Data,HEX);
    Serial.print(" ");
    Serial.print(myBytes[j].B.Hi,HEX);
    Serial.print(" ");
    Serial.println(myBytes[j].B.Lo,HEX);
  } 
}

void loop()
{

}

I would also like to point out a new milestone, that this is the very first time I have ever had a reason to use a Union.
Sad, I know.

I saw a few functions that used the above method. Makes sense.

I found that for little data arduino has a built in function already.

For example: tft.print((lowByte(temp))); tft.print(" "); tft.print(highByte(temp)));

Here is an example which uses nice property of shift.

Now please explain how would using union improve the code or why is it necessary ?

The Data is unchanged , so this flow

byte bByte = Data >> 8; .... save hi byte bByte = Data & 0x00FF; .... save Lo byte

reuses same bByte variable anyway.

Cheers Vaclav

int Data = 12345;
byte Hi = Data >> 8;
byte Lo = Data & 0x00FF;
Serial.println(Data,HEX);
Serial.println(Hi,HEX);
Serial.println(Lo,HEX);
Serial.println(Data,HEX);

You don't need to mask the low order byte. This works.

byte Lo = Data;

Pete

Now please explain how would using union improve the code or why is it necessary ?

I said it was another option and it can handle multiple types. Good for future practices.

Yes, it was mistake to miss the simplicity as right shift, and yes, I was looking for a reason to finally use a union.

Now please explain how would using union improve the code or why is it necessary ?

You can use the union method to convert a float into four bytes and save it. I doubt this would work using the alternatives.

el_supremo: You don't need to mask the low order byte. This works.

byte Lo = Data;

Pete

Even better, nice and smart way showing you know your stuff. Still not fully convinced that one needs union to convert say 32 bits to bytes. Actually I am building 40 bits word from bytes byte by byte anyway. Cheers Vaclav