# Need routine to convert decimal to hex numbers

Status
Not open for further replies.

#### ZeleC

##### Full Member level 5 decimal to hex

hello guys
been working on hc11 and i want to convert the decimal numbers entered by the keypad to hex
so i need a routine for decimal to hex
any help out there

S

#### sick_man

##### Guest decimal too hex >>> lol to convert decimal too hex from a keypad >?
it will be in total a signed or unsigned byte
as an hc11 is only small bits core

so in the range -127 too +127 for 00 - ff
if you want too read the true decimal value

then that is the same ..... as if you sign the -127 - +127 too give in range instead 0-256
256 being ff

just declare as a unsigned byte and convert

#### ZeleC

##### Full Member level 5 I KNOW THAT IS EASY FOR ONE BYTE
BUT WHAT IF I WANT TO CONVERT 2000 TO HEX :?

##### Member level 5 I think that he want something different.
Let's suppose that we have in the keyboard buffer something like "123" (three ASCII chars or some other form of keyboard scancode) and we need to convert them to 0x7B in byte variable.

The C language routine is simple, but I do not know HC11 assembler.

#### ZeleC

##### Full Member level 5 HEY I NEED THE ROUTINE FOR LARGE NUMBERS
I KNOW THE CONVENTIONAL WAY
2000=2*16^3+0*16^2+....
BUT IM LOOKING FOR ANOTHER ONE
MAYBE AN EASIER WAY THAT DOESNT EATS TO MUCH TIME IN THE uC
THX

#### papyaki Divide your number by 16^0 (1). If the result is > 16 then divide your decimal number by 16^1 (16). If the result > 16 then divide your decimal number by 16^2 (256). If then result is > 16 then divide your decimal number by 16^3 (4096). If the result is > 16 divide your decimal number by 16^4 and so on until the result is < 16.

For example for 2000, you can divide it by 16^2 (256). 2000/256 = 7,8125.

Now you must take the entire part of 7,8125 (7) and multiply it by 16^2 (256) : the result is 7*256 = 1792.

Substract this result from 2000 : 2000 - 1792 = 208.

Then divide 208 by 16^1 (16) : 208/16 = 13 (0xD hex).

Substract 13*16 from 208 : 208 - 208 = 0

Divide 0 by 16^0 : 0/1 = 0

The final result in hex is : 7D0 ((7*16^2) + (13*16^1) +(0*16^0))

Try another example with 10001 decimal

10001/16^3 = 2,44.... (Entire value is 2)

2*16^3 = 8192

10001 - 8192 = 1809

1809/16^2 = 7,06... (Entire value is 7)

7*256 = 1792

1809-1792 = 17

17/16^1 = 1,06... (Entire value is 1)

1*16^1 = 16

17-16 = 1

1/16^0 = 1 (Entire value is 1)

The final result is 0x2711 (hex value).

Not very complicated...

#### papyaki Sorry Zelec, your last post arrived while i wrote mine...

##### Member level 5 Zelec: I do not understand one thing.
You want to use the numbers entered on the keyboard for some internal calculations or you just want to display them in hex.

If you want to use them, let's suppose that you have the keycodes from keyboard in some buffer, ending with CR or some other special character.
Firstly adjust the bytes in the buffer (in 99 percent it is some kind of substraction). You want to have 0 for '0', 1 for '1' ... 9 for '9'.
After that, simply make a loop and multiply and add bytes in the buffer from (right-1) to left by power of ten.

Let's suppose that you have "12345\n" in the buffer. So adjust and in the loop multiply and add:

5x1 + 4x10 + 3x100 + 2x1000 + 1x10000 etc.

If you want, you do not need multiplication. You can do it just with shifts and additions and some more loops. Because if you want to multiply by 10, it is (number<<3) + number + number.

#### Gomez

##### Full Member level 2 Re: decimal to hex

ZeleC said:
hello guys
been working on hc11 and i want to convert the decimal numbers entered by the keypad to hex
so i need a routine for decimal to hex
any help out there

Sorry is for Z80

Decimal -> Dual

(DE).4 8-digit packed decimal number
(HL).4 4-byte result

DEBU1: PUSH BC
LD B,20 ;=32D, cycles counter
DED11: XOR A ;<CY>=0
CALL DH4 ;devision into two parts
CALL RR4 ;4-Byte shift to right
DJNZ DED11
POP BC
RET

RR4: PUSH BC
PUSH HL
LD B,4
RR41: RR (HL)
INC HL
DJNZ RR41
POP HL
POP BC
RET

DH4: LD C,4
DHN: PUSH BC
PUSH DE
DHN1: LD A,(DE)
RRA
PUSH AF
BIT 3,A ;handling carried
JR Z, DHN2
SUB 3
DHN2: CP 80H ;handling carried
JR C,DHN3
SUB 30H
DHN3 LD (DE),A
POP AF
INC DE
DEC C
JR NZ,DHN1
POP DE
POP BC
RET

Gomez

Status
Not open for further replies.