News:

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

hex to ascii without library support help.

Started by marco_xx, February 20, 2007, 10:53:19 AM

Previous topic - Next topic

marco_xx

Hi,

i have managed to create a lib wich uses no lib funcs and imports everything runtime so my exe is only 4KB ;-)
I have a compression routine but for some reason i can`t seem to get hex/ascii thing working.

for example:


buffer db 256 dup(0)
nasty2 db 00h, 40h, 0C0h, 80h, 4Ch, 00h, 00h, 00h,  0B4h, 0BDh, 59h, 0ADh, 1Ch, 0B4h, 0BDh, 59h,0ADh, 1Ch, 64h, 0E3h



; doesn f***ing work

do_it proc x_nasty,i_size

local abuf:byte

;NastyToHex:

push offset nasty

push edi


;mov eax,OFFSET nasty  ; input
;mov edx,OFFSET buffer ; output
;mov edi,8             ; number of bytes to convert

;assume eax:byte
mov eax, x_nasty  ; input
mov edx, OFFSET buffer ; output
mov edi,i_size  ;8             ; number of bytes to convert



_ntx_loop:
mov cl,byte ptr [eax] ; convert top half
and cl,0F0h
shr cl,4
cmp cl,9
jle no_xtra_add
add cl,7
no_xtra_add:
add cl,30h
mov byte ptr [edx],cl

mov cl,byte ptr [eax] ; convert low half
and cl,0Fh
cmp cl,9
jle no_xtra_add2
add cl,7
no_xtra_add2:
add cl,30h
mov byte ptr [edx+1],cl

inc eax
add edx,2

dec edi
jnz _ntx_loop

pop eax
mov eax,OFFSET buffer



ret


do_it endp


invoke  do_it,  offset nasty2 ,n2len
invoke MessageBox, NULL,addr nasty2, addr buffer, MB_OK



it outputs: 0101deeh11h and not ( Life is Wonderfull for example).
I am planning compressing API-names(strings) and storing them as hex DB  and making the smallest exe possible.

I already try`d the masmlib but to be honnest it does not work for me.
When i was searching the forum i did download everything related to HEX_TO_ASCII but i still didn`t see any good routine.

maybe someone can shine some light on it.

Thnx







hutch--

marco,

The routines in the masm32 library for byte data to hex string were designed for speed rather than being compact.


bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD
hex2bin proc src:DWORD,dst:DWORD


They both use tables for the conversions which brings their size up.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

marco_xx

hmm this seems tobe hard, converting:
nasty2 dw 00h, 40h, 0C0h, 80h, 4Ch, 00h, 00h, 00h,  0B4h, 0BDh, 59h, 0ADh, 1Ch, 0B4h, 0BDh, 59h,0ADh, 1Ch, 64h, 0E3h
n2len equ 20

to Ascii_string.

The

;bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD
;hex2bin proc src:DWORD,dst:DWORD

invoke hex2bin, offset nasty2,offset buffer
;invoke HexDump,offset nasty2,n2len,offset buffer

;invoke dw2a,offset nasty2,offset buffer


functions all fill the buffer with HEX or bin not ascii`s.
can somebody give me a sample like

myhex 0h,1h,2e
text_converted db 256 dup(0)


invoke convert,myhex,buffer


please?








dsouza123


.486                       ; create 32 bit code
.model flat, stdcall       ; 32 bit memory model
option casemap :none       ; case sensitive

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\masm32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
  buffer db 256 dup(0)
  nnnnnn db 00h, 40h, 0C0h, 80h, 4Ch, 00h, 00h, 00h, 0B4h, 0BDh, 59h, 0ADh, 1Ch, 0B4h, 0BDh, 59h,0ADh, 1Ch, 64h, 0E3h

.code
start:
  mov esi, sizeof nnnnnn - 1
  mov edi, 0
  .while (edi <= esi)
     mov dl, [nnnnnn+edi]
     mov al, dl
     shr al, 4
     and al, 0Fh
     add al, 30h
     .if (al > 39h)
        add al, 7    ; uppercase  or 7+32 for lowercase
     .endif
     mov [buffer+edi*2], al

     mov al, dl
     and al, 0Fh
     add al, 30h
     .if (al > 39h)
        add al, 7    ; uppercase  or 7+32 for lowercase
     .endif
     mov [buffer+edi*2+1], al
     inc edi
  .endw

  invoke MessageBox, NULL,addr nnnnnn, addr buffer, MB_OK     ; nnnnnn has a leading zero byte so no message shows, just caption
  invoke ExitProcess,eax
end start

[attachment deleted by admin]

dsouza123

An array of bytes used as an ascii string is treated as an zero (byte) terminated string,
so if the first byte value is 0 ( 00h as shown in the listing) the string will terminate right there,
ie an empty string so nothing is shown in the messagebox output.

sinsi

Instead of this
      and al, 0Fh
     add al, 30h
     .if (al > 39h)
        add al, 7    ; uppercase  or 7+32 for lowercase
     .endif

try this
    and al,15
    cmp al,10
    sbb al,105
    das

Light travels faster than sound, that's why some people seem bright until you hear them.

marco_xx

Thanx.

I am sorry maybe i didn`t explain it well enough.
I feel so ...... n00b :p

okay another try.
lets asume i have encoded the word  'test' using hexdump.exe

.data


encoded_txt db 74h, 65h, 73h, 74h ; =test

is it then possible to take the encoded_text and CONVERT it to a string
wich MessageBox can display?
for example:

invoke HexToAscii,encoded_text,outbuffer
invoke messagebox,0,some_title,outbuffer,0


Thanx for helping me.
I really apreciate all the help


Marco








dsouza123

Sure just have a zero byte after it.

.data
encoded_txt db 74h, 65h, 73h, 74h ; =test
zterm db 0

.code
invoke MessageBox, NULL,addr encoded_txt, addr encoded_txt, MB_OK

marco_xx


tenkey

It works, but not in the way you've tried in your first attempt.

Your first attempt requires hex notation in character or text format.

encoded_text db '74657374'

After conversion, you still need to add a zero byte if you don't want MessageBox to print garbage after the first four characters.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8