# given a hex number, how to recover the bytes?

upperByte = 4c

lowerByte = 02

4c << 8 = 4c00

4c00+02 = 4c02

4c02 >> 8 = 4c

4c02 & 7f = 2

2 << 8 = 200

(4c | 200)+1 = 24D which is 589 in hex

I have tried to figure it out but get stumped on the first step after -1. Basically though I'm trying to figure out the 2 bytes from the hex number

some people are having problems figuring out the question, so I will reiterate it (hopefully clearer).
this equation takes 2 bytes 4c and 02 and turns them to a hex number 24d. i need the reverse, figuring out what the 2 bytes are from the hex number.

-
is this homework... please tag as appropriate –  sehe Dec 9 '11 at 16:18
In lines 5 and 6 you have unpacked the two bytes of the number successfully, so I don't understand what the question is. –  Ross Millikan Dec 9 '11 at 16:21
no its not homework, havnt been in school in a while. my question is how to do the reverse, take the 24d (hex value) and turn it back into the 2 bytes 4c,02 –  owen gerig Dec 9 '11 at 16:25

Since this is more of a programming question, I'm going to use "C notation" for the hex values.

Combining 2 bytes to get a 16-bit integer:

theInteger = (upperByte << 8) + lowerByte;


For example,

upperByte = 0xfc
lowerByte = 0x92
theInteger = (upperByte << 8) + lowerByte
= (0xfc << 8) + lowerByte
= ( 0xfc00 ) + 0x92
= 0xfc92


dissecting a 16-bit integer into two bytes:

upperByte = theInteger >> 8;
lowerByte = theInteger & 0xff;


For example,

theInteger = 0xfc92
upperByte = theInteger >> 8
= 0xfc92 >> 8
= 0xfc
lowerByte = theInteger & 0xff
= 0xfc92 & 0xff
= 0x92.


formula

I suspect the "& 7f" in that formula should be something more like "& ff". If it were really "& ff", then reversing the formula is easy -- subtract one from the integer, and then dissect the integer into two bytes.

However, if the "& 7f" is correct, then it's a little more complicated -- no matter what initial byte values we have, that formula (with the "& 7f") will never generate certain integers. In particular, it will never generate the integer 0x8000. Also, there are two different byte pairs that that formula (with the "& 7f") will generate the same final value. In particular, the byte pair upperByte=0x4c and lowerByte=0xff gives the final integer value 0x7f4d, the same as upperByte=0x4c and lowerByte=0x7f.

Is that really what you wanted?

-

If I understand your question, your "formula" is: $$y=x_{up}|(x_{low} \ll 8)+1$$ and the "reverse" would be $$\left\{\begin{array}{l}x_{low}=(y-1) \gg 8\\x_{up}=y-x_{low}\end{array}\right.$$ with, in your example, $$x_{up}={\tt 4C},\; x_{low}=2\quad \text{and}\quad y={\tt 24D}.$$ However, this only makes sense if $y$ is two bytes long, and it's not clear to me if you mean the original operation to be performed in this order or permuting the values for "lower" and "higher" bytes.

This is similar to asking for the units and tens in decimal number 42, which is more of a programming question than a maths question.

(edited to correct typo in the example value of y)

-
wow i need some time to digest this answer and really understand it. in response to your question i looked up the word permuting and dont really grasp the meaning in the context of the sentence. but basically im trying to get 4c,02 from 24d by reversing the steps. the answer to my example problem should be 4c,02 from 24d. –  owen gerig Dec 9 '11 at 18:46