Hi there,

i have an litte problem.......maybe its an stupid question but....
i want to convert an byte to an 2 digit hex chars.

my source code looks like this....
``````
xor edx, edx
;al = current char
mov ecx, 16
div ecx
.if dl > 9h
.else
.endif

mov byte ptr[edi], dl
``````

the problem is that he only shows the second hex digit he doesnt show the first :-/
but i dont khnow how to edit the source to show both......
i khnow ist stupid but can anybody help me with this problem?
i had tryed it with an word ptr but it still shows the same.....

thanks
Posted on 2004-07-02 05:00:15 by Tenshi
search button.. use it
Posted on 2004-07-02 05:02:56 by evlncrn8
i cant find something usefull.....i think iam to stupid ;-) can anybody help me please?
Posted on 2004-07-02 05:52:38 by Tenshi

``````
xor edx, edx
mov ecx, 16
div ecx
.if dl > 9h
.else
.endif

.if al > 9h
.else
.endif

mov byte ptr[edi+Pos], al
inc Pos
mov byte ptr[edi+Pos], dl
``````
Posted on 2004-07-02 07:05:09 by Tenshi
Well you could use the following table:
0123456789ABCDEF
Then using each nibble in the byte as an index into the table you can find you character and place it in the string buffer:
``````
;al has byte
mov dl,al              ;Save a copy
and al,0Fh
shr dl,4
lea edi,table         ;Hex table
and dl,0Fh
movzx eax,al
mov al,[edi+eax]  ;AL has low nibble
movzx edx,dl
mov ah,[edi+edx] ;AH has high nibble

``````

This code hasnt been tested but I used this method before to convert values.
Posted on 2004-07-02 08:07:00 by x86asm
``````;assumes byte already in AL
;and EDI points to memory where to store the converted byte
rol  ax,4        ;high nibble of byte in low nibble of AH
ror  al,4        ;low nibble of byte back to low nibble of AL
add  ax,3030h    ;convert both to ascii
.if   al > "9"   ;low nibble of byte
.endif
xchg al,ah
.if   al > "9"   ;high nibble of byte
.endif
stosw            ;the high nibble will be stored as 1st character
;and EDI will point to the byte following the two characters``````
Raymond
Posted on 2004-07-03 20:55:48 by Raymond
hex2ascii:

push ebx
mov ebx,eax
ror ebx,04h
and bl,0fh
cmp bl,09h
ja \$+07h
jmp \$+05h
and al,0fh
cmp al,09h
ja \$+06h
jmp \$+04h
mov ah,bl
xchg ah,al
pop ebx
ret
Posted on 2004-07-04 00:11:02 by mrgone
``````
hextab db "0123456789ABCDEF"

movzx   edx, al         ; Save a copy
and     eax, 0Fh        ; need to use eax as index later, mask out high bits
shr     edx, 4
mov     ecx, hextab     ; nasm/fasm syntax, add "offset" for masm
mov     al, [ecx+edx]   ; AL has low nibble
mov     ah, [ecx+eax]   ; AH has high nibble
ret
``````

39 bytes, only clobbers clobberable :) registers. Hex table can be re-used if you need a high-performance version working on multiple bytes. And there's probably even niftier ways to do this (including the old cmp+sbb+das trick, but that's slow.)
Posted on 2004-07-04 00:26:59 by f0dder
Hi f0dder, I fixed a bug in your code, now it should work.

``````movzx   edx, al         ; Save a copy
and     eax, 0Fh        ; need to use eax as index later, mask out high bits
shr     edx, 4
mov     ecx, hextab     ; nasm/fasm syntax, add "offset" for masm
mov     al, [ecx+eax]   ; AL has low nibble
mov     ah, [ecx+edx]   ; AH has high nibble``````
Posted on 2004-07-07 14:50:48 by bszente