PDA

View Full Version : negative HEX to bytes



cpayne
- 16th October 2008, 22:55
How do I take a HEX value from -255 to 255 and convert it to a byte from 0 to 255 with another bit of 0 or -1 for the sign? so HEX 81 (-127) is 127 with a sign of 1 and HEX 7F (+127) is 127 with a sign of 0.

Thanks

skimask
- 16th October 2008, 23:13
How do I take a HEX value from -255 to 255 and convert it to a byte from 0 to 255 with another bit of 0 or -1 for the sign? so HEX 81 (-127) is 127 with a sign of 1 and HEX 7F (+127) is 127 with a sign of 0.

Thanks

What version of PBP are you using?
Google 'Two's Complement arithmetic'

cpayne
- 16th October 2008, 23:24
I think its 2.50A PBPro

skimask
- 16th October 2008, 23:56
I think its 2.50A PBPro
If you're using 2.50A, and an 18F, then you can use LONG's, which will handle signed integers, both in the math and in the display.
But you want to take a -255 to 255, with a range of 512 integer values, and fit that into a byte value, which has a range of 0 to 255, a total of 256 integer values.
Do you want to lose that 'resolution'? Or do you actually mean something else?

RussMartin
- 17th October 2008, 18:50
How do I take a HEX value from -255 to 255 and convert it to a byte from 0 to 255 with another bit of 0 or -1 for the sign? so HEX 81 (-127) is 127 with a sign of 1 and HEX 7F (+127) is 127 with a sign of 0.

I'm unsure of how you wish to implement the data, so I may be out of line here.

But have you considered using an offset? Consider 127 to be 0, then manipulate the numbers from there. Negative values are 127-x; positive values (i.e., if x>127) are x-127.