Hi,

I have a char, it's "61", now i want to convert into the hex of it ,"3D" in this case for "61".

So how can I convert a char(it's alaways a number from 0-61) into it's hex value?

:alright:
Posted on 2004-08-06 16:07:32 by flapper
Hi flapper


You can use wsprintf, with the following format specification:

lx, lX Long unsigned hexadecimal integer in lowercase or uppercase.

That should do the trick :)



regards,


The SharK
Posted on 2004-08-06 16:15:00 by The SharK
thanks, is there another way? Without the use of an API call?
Posted on 2004-08-06 16:19:09 by flapper
thanks to you, how can I use this code?

Edit: why you deleted your post donkey?
Posted on 2004-08-06 16:29:40 by flapper
You can use a lookup table - either a 0-255 entry table of 2xChar, or a 0-15 (nibble) table (requires slightly more code).

http://www.asmcommunity.net/board/index.php?topic=18766
Posted on 2004-08-06 16:31:40 by f0dder
offtopic: Is a char with "61" the same as an integer with "61" ?
Posted on 2004-08-06 16:34:50 by flapper
A char is 8 bit.

61 is TWO char's made of 0x36 and 0x31.
Posted on 2004-08-06 16:36:54 by The SharK
Sorry flapper there was a mistake in it. this should do it for ya. Good from 0-99

hextab	DB "0123456789ABCDEF"


xor eax,eax
mov al,[szDec]
sub al,30h
mov dl,[szDec+1]
or dl,dl
jz >
mov ecx,10
mul cl
sub dl,30h
add al,dl
:
and eax,0FFh

xor edx,edx
mov edi,offset hextab
movzx ecx,al
shr ecx,4
mov dl,[edi+ecx]
mov [szHex],dl
movzx ecx,al
and ecx,0fh
mov dl,[edi+ecx]
mov [szHex+1],dl
Posted on 2004-08-06 16:37:52 by donkey
okay, thanks.
@f0dder : Im a complete noob, can you give me an example?

@donkey : Where do I tell that code what my CHAR is?
Posted on 2004-08-06 16:39:32 by flapper
The 2 byte ascii decimal string is in szDec, it must be null terminated. The output is into szHex.

For example

szDec DB "61",0
szHex DB 4 DUP (?)

Will output :

szHex = "3D"
Posted on 2004-08-06 16:45:01 by donkey
Thanks, it worked :alright:
Posted on 2004-08-06 16:59:02 by flapper
One problem:

I call a function that will return this number (61 in this case).

when I do mov szDec, eax i get an error
Posted on 2004-08-06 17:13:07 by flapper
You have to move it to an undefined string.

You're trying to move a value to a defined string.


Use szHex instead :alright:
Posted on 2004-08-06 17:15:44 by The SharK
If the number you wish to convert is already in DWORD format just call the routine starting at the following line. Leave the number in EAX and there is no need for szDec. And next time as a little suggestion do not enclose the number in quotes, I thought you meant it was in string format.

mov edi,offset hextab

movzx ecx,al
shr ecx,4
mov dl,[edi+ecx]
mov [szHex],dl
movzx ecx,al
and ecx,0fh
mov dl,[edi+ecx]
mov [szHex+1],dl
Posted on 2004-08-06 17:17:32 by donkey
lol yes it is in string format, I call a function it returns me for example "61", it's a char, same like "ABC-XYZ"

MY problem is this: invalid instruction operands

.data?
szDec DB ?
szHex DB 4 dup (?)

.code

call dword ptr <- Function that will return the CHAR
mov szDec, eax <-- There's the error

after this I want to convert the CHAR MyFunction returned to HEX and write it into a memory position:

mov esi, 01020304
mov eax, szHex <-- Same error again : invalid instruction operands
mov dword ptr , eax
Posted on 2004-08-06 17:55:12 by flapper
First off function calls are always DWORDs so why waste typing telling the compiler what it already knows...

call dword ptr is the same as call

However, because you are using MASM and it has stricter types than other assemblers you must type cast the buffer. This is where you actually need DWORD PTR...

mov DWORD PTR szDec, eax

same here...

mov eax, DWORD PTR szHex
Posted on 2004-08-06 18:17:29 by donkey
:) Now I have this error:

mov esi, 01020304
mov eax, dword ptr
mov byte ptr , eax <-- invalid instruction operands
Posted on 2004-08-06 18:55:22 by flapper
EAX is a DWORD, you cannot mov it as a BYTE. Use...

mov , al

Since AL is a byte sized register there is no need to specify BYTE PTR with an untyped memory location (one that is pointed to by a register).
Posted on 2004-08-06 19:05:13 by donkey
Works.

Just if I look in the program where I wrote the Hex in, it's always another value, sometimes 51, sometimes 55 decimal :(
Posted on 2004-08-06 19:20:25 by flapper
It doesn't seem to work properly, when the first function return 0 the hex value is something for 50 in decimal, never the same, weird!
Posted on 2004-08-06 19:46:45 by flapper