How can i convert a short to 2 bytes?

Like say i want to convert xxxxx(short) to 2 bytes xxx,yyy.
(in other words, i have a variables that is a short and i want to get the 2 bytes equivalent of it)
I'm not quite sure how to do this. Some help?

http://www.arduino.cc/en/Reference/HighByte

BTW, if you didn't have any "yyy" to begin with, this still won't work :wink:

Ah thanks!

Serial.write(highByte(val),BYTE);
Serial.write(lowByte(val),BYTE);

Gives the error

error: invalid conversion from 'uint8_t' to 'const uint8_t*

_< What am I doing wrong here?

You're trying to use a method that is supposed to be used for arrays for scalar quantities.

OH! XD My mistake. XD Thanks!

The classic answer is (short && 0xFFFF) and (short >> 8), with some consideration for signed numbers (the short should be unsigned)

Two Fs, brtech. :slight_smile:

short myshort = 0x9A76;

byte low = (myshort & 0xFF);
byte high = (myshort >> 8) & 0xFF;

This gives low == 0x76 and high == 0x9A.

Gak! You are correct of course.

.. or no F's at all

short myshort = 0x9A76;

byte low = (byte)myshort;
byte high = (byte)(myshort >> 8);

The F's are only needed if you want to convert a short hibyte/lobyte to another short (or longer). The values low/high in the above case can not hold any values beyond 8 bits so filtering higher order bits is not needed.