Hi all. I need some help with my ordeal.

I need to convert an integer to hex then to reverse hex.

Do anyone know how?


Thanx in advance
Tsongkie
Posted on 2002-01-29 16:44:57 by Tsongkie[ii]
check the conversion procs in the masm32 lib. should be called dw2hex / hex2dw or something :)

.pix

---
// kamusta, kabayan?
Posted on 2002-01-29 16:50:27 by pixelwise
Hi!
I've coded a small proc to convert any DWORD to a hex-string.
It is written for Tasm32!

Call Int2Hex, eax, offset szHexStr ;Example

Int2Hex PROC
ARG Value:DWORD, Buffer:DWORD

PushAD
Mov eax, Value
Mov ecx, 8
Mov edi, Buffer

Int2HexLoop:
Mov edx, eax
And edx, 0Fh
Mov dl, byte ptr
Mov byte ptr , dl
Shr eax, 4
Loop Int2HexLoop
Mov edi, Buffer
Mov byte ptr , 0
PopAD
Mov eax, Buffer
Ret
HexTable:
DB "0123456789ABCDEF"

Int2Hex ENDP

I think it is very easy to do that algo reversed..
Use "Inc ecx" and "Cmp ecx, 8 - Jne ..." instead of "Loop ..."
Initialize ecx with zero (Xor ecx, ecx)

Good Luck
Posted on 2002-01-29 16:56:05 by Rennsemmel
If you need it padded ie. 0x1 reverses to 0x10000000 (ie 32bit padded hex value), then why not reverse the number, then convert it to text...



mov eax, myNum
bswap eax
mov ecx, eax
shr ecx, 4
and eax, 0F0F0F0Fh
and ecx, 0F0F0F0Fh
shl eax, 4
or ecx, eax
; result in eax, convert it to decimal, or hex in your prefered manner


Mirno
Posted on 2002-01-29 17:07:16 by Mirno
Hey thanks for all the reply. And I think that Mirno solved my problem. Thanx mate! I'm really kinda new at this stuff, can you comment a little on that code, it solved my problem but I dont know how it works ;) sorry for being such a pain. If i'll use it, I might as well understand it, right? Thanx


And to kabayan pixelwise: musta kabayan? :)
Posted on 2002-01-29 17:27:07 by Tsongkie[ii]
Again, i'm faced with another problem:

in pascal/delphi there is a function just like writeln and readln. It writes and reads strings into a file. Is there such a function in MASM? or any code out there that might help me? thanx again
Posted on 2002-01-29 17:37:37 by Tsongkie[ii]
You can use Windows-API:

CreateFile to get a handle
WriteFile to write into the file
ReadFile to read from file
SetFilePointer to set the filepointer :)

look into some reference like MSDN or Win32.hlp
Posted on 2002-01-29 17:49:16 by Rennsemmel
Hey thanx for the reply. But i already have that. What I want is to read a text file line by line.
Posted on 2002-01-29 17:52:16 by Tsongkie[ii]
oh..I never need that..but I know whats to do (maybe not the best way):

Stream a file into a buffer byte by byte

Check every byte after reading for ASCii 13 (ascii 13 followed by ascii 10 means linebreak)

If you found ASCii13, you'll have a whole line into your buffer (zero terminate it)

Increase file-pointer by one (you know there is the ascii 10 for the linebreak, too)

loop this until you have reached the end of the file!

----
Another way (faster) is to read the whole file into your buffer and search for ascci 13 using "Repne Scasb" or something like that.
For example you can replace ASCii 13 with Ascii 0 (Zero-termination). Maybe create a LinePointer-array which points to the start of every line.

I hope I explained it clearly.
Posted on 2002-01-29 18:17:43 by Rennsemmel


mov eax, myNum

bswap eax
; Full byte-swap (ABCD->DCBA byte order).

mov ecx, eax
shr ecx, 4
and eax, 0F0F0F0Fh
and ecx, 0F0F0F0Fh
shl eax, 4
or ecx, eax
; A nibble swap -
; Copy the source
; Mask lower nibbles in eax
; Mask upper nibbles in ecx
; Shift so they are in oppsite positions
; - (half the shifting is done before the masking).
; Or the results back together

; result in eax,
; convert it to decimal, or hex in your prefered manner


So:
eax = 12 34 56 78h

bswap eax
eax = 78 56 34 12h

ecx = eax

shr ecx, 4
ecx = 07 85 63 41h

and eax, 0F 0F 0F 0Fh
eax = 08 06 04 02h

and ecx, 0F 0F 0F 0Fh
ecx = 07 05 03 01h

shl eax, 4
ecx = 80 60 40 20h

or eax, ecx
eax = 87 65 43 21h

Done!

Mirno
Posted on 2002-01-30 04:24:39 by Mirno