PDA

View Full Version : HEX numbers



Charles Linquis
- 16th April 2006, 06:35
Is there an easy way to separate the "digits" of a hex number?
For example 63251 = F713, but I need to separate out each digit
as ASCII into an array element.
e.g.

NumArray[0] = "F"
NumArray[1] = "7"
NumArray[2] = "2"
...

I know there must be an elegant way, but I can't think of it.

sougata
- 16th April 2006, 07:29
Hi,

Have a look http://66.13.172.18/techref/microchip/math/radix/index.htm

paul borgmeier
- 16th April 2006, 07:50
I don't know if it is elegant but ...

X var WORD
Y var BYTE
NumArray var BYTE[4]

X = 63251

Y = X.lowbyte & $0F ' isolate the lower nibble
gosub ConvertAscii
NumArray[3]=Y ' ="3"

Y = X.lowbyte >> 4 ' move high nibble to low nibble
gosub ConvertAscii
NumArray[2]=Y '="1"

Y = X.highbyte & $0F ' isolate the lower nibble
gosub ConvertAscii
NumArray[1]=Y '="7"

Y = X.highbyte >> 4 ' move high nibble to low nibble
gosub ConvertAscii
NumArray[0]=Y '="F"

END

ConvertAscii:
If Y < 10 then
Y = Y + 48
Else
Y = Y + 55
Endif
Return

Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

Charles Linquis
- 17th April 2006, 00:09
After I posted I realized that I had the answer all along. I wrote an Intel Hex routine awhile back. It dealt with bytes only. All I had to do was to use the routine twice - very similar to your approach.

Thanks for responding!