# 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;

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.

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.89215183,"math_prob":0.8596421,"size":912,"snap":"2021-43-2021-49","text_gpt3_token_len":263,"char_repetition_ratio":0.12885463,"word_repetition_ratio":0.0,"special_character_ratio":0.32456142,"punctuation_ratio":0.20179372,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9589648,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-25T15:47:42Z\",\"WARC-Record-ID\":\"<urn:uuid:8318d710-16c3-49f4-aaf8-2b4105e9cef3>\",\"Content-Length\":\"43498\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e601f203-3778-44a8-bb7f-145634c13e3c>\",\"WARC-Concurrent-To\":\"<urn:uuid:676be166-3077-42f6-b0e5-287c8f7461cf>\",\"WARC-IP-Address\":\"184.104.202.141\",\"WARC-Target-URI\":\"https://forum.arduino.cc/t/variables-convert-16-bit-into-2x-8-bit-and-vice-versa/254548\",\"WARC-Payload-Digest\":\"sha1:67PUITNR6AS7EZ4BCHMWCY74M6AYN4JE\",\"WARC-Block-Digest\":\"sha1:ABSZ4K6KE4OPELRSJKCU2VA4ZKGHSUWU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587719.64_warc_CC-MAIN-20211025154225-20211025184225-00504.warc.gz\"}"}