News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How to convert a long decimal number to hexdecimal

Started by Pink, December 11, 2008, 02:27:42 PM

Previous topic - Next topic

Pink

Hi,
This is my first message and I'm nice to meet you all in this forum, my question is : how can I convert a long nomber ( more than 8 chars) to hexdecimal?

If my long number (123456789) is shown in the registries as: Eax = 12345678 Edx=9000000 how can I convert it in an hexdecimal number?

Thanks for answers,
Pink

donkey

Well, to start off with your number is not in "decimal" if it is contained in a register, it is in binary. Binary to Hex conversion requires no math. Each 4 bits of binary represents 1 hexadecimal digit so you can use a lookup table to do the translation, for most common purposes they are fast enough ("0123456789ABCDEF").
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Mark Jones

Hello Pink and welcome to the forum. Please try "hexadecimal" in the search box above -- this returns three pages of good posts. If you get stuck, just let us know.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Vortex

Hi Pink,

Have a look at masm32.lib   dw2hex does the job :

Quotedw2hex

dw2hex proc source:DWORD, lpBuffer:DWORD

Description

dw2hex converts a DWORD value to an ascii hex string.

Parameters

1. source The DWORD value to convert

2. lpBuffer The address of the buffer to receive the ascii hex string

Return Value

There is no return value.

Comments

The buffer for the result needs to be 8 bytes long plus one byte for the terminator so for alignment, make the buffer 12 bytes long.

donkey

As Vortex said dw2hex will work but to use it with EDX:EAX you will have to do it in 2 passes...

.data
outbuf db 32 DUP (0)

.code
mov edi, offset outbuf
invoke dw2hex, EDX, EDI
add edi, 8
invoke dw2hex, EAX, EDI


Note that this assumes that the high order DWORD is in EDX, which is the preferred way to represent a 64 bit value in 32 bit registers since many x86 instructions require that large values are passed that way.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

Quote from: donkey on December 11, 2008, 08:01:11 PM
As Vortex said dw2hex will work but to use it with EDX:EAX you will have to do it in 2 passes...

.data
outbuf db 32 DUP (0)

.code
mov edi, offset outbuf
invoke dw2hex, EDX, EDI
add edi, 8
invoke dw2hex, EAX, EDI


Note that this assumes that the high order DWORD is in EDX, which is the preferred way to represent a 64 bit value in 32 bit registers since many x86 instructions require that large values are passed that way.

You might insert a push/pop:
include \masm32\include\masm32rt.inc

.data?
outbuf db 32 DUP (?)

.code
start:
mov edx, 12345678h
mov eax, 90123456h
mov edi, offset outbuf
push eax
invoke dw2hex, EDX, EDI
add edi, 8
pop eax
invoke dw2hex, EAX, EDI
sub edi, 8

MsgBox 0, edi, "Hello edx and eax!", MB_OK
exit

end start

donkey

QuoteYou might insert a push/pop:

True, didn't actually try the code, too simple to bother starting up the IDE for.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Pink

Uhm, maybe I was not so clear, I retry:
if I have edx=12345678h and eax=90000000h the dw2hex used in this way:

mov edi, offset Outbuf
push eax
invoke dw2hex, EDX, EDI
add edi, 8
pop eax
invoke dw2hex, EAX, EDI
sub edi, 8

will give me in the OutBuf the string "123456789000000", but this is not what I meant with "conversion", I meant this:

decimal 123456789 ->  hex 75BCD15h

so the user enters "123456789" in the dialog box, I succeeded then to obtain eax = 12345678h and edx=90000000h, now what I want is to have in an buffer the result: 75BCD15h , how can I do it?

Otherwise, if you know how to obtain the hex number from a decimal number entered by the user, just speak =)

Thanx for the answers and sorry for the late in me reply, you know..xmas holydays =)

cmpxchg

Quote from: Pink on December 28, 2008, 09:00:16 PM
so the user enters "123456789" in the dialog box, I succeeded then to obtain eax = 12345678h and edx=90000000h, now what I want is to have in an buffer the result: 75BCD15h , how can I do it?

If user enters "123456789" then that's ascii. And  if you consider that to be decimal then you first need to convert decimal ascii 2 dword (function a2dw  I believe) and thats gonna be eax=0x75BCD15=123456789=111010110111100110100010101b. Then you can convert DWORD to hexadecimal ascii representation using dw2hex.

I don't know where you found long here because if 75BCD15h & 123456789 are the same numbers then they will fit into same 4byte register

MichaelW

Pink,

In the example code, specifying the input numbers in hex instead of decimal makes it easy to verify that the hex results are correct. If the user will be specifying the inputs in decimal, then as cmpxchg stated, you will need to get the input values by converting the decimal ASCII strings, and then convert the values to hex. The MASM32 library includes three different procedures that can be used to convert a decimal ASCII string to a value, atodw, a2dw, and atol. I think any of them should work for this, but you should be aware that atol will accept a leading sign and convert the value accordingly.
eschew obfuscation

Pink

ok Thanks,
atodw works, a2dw gives me a weird result, but ok, I solved the problem =)
Thanks Again