News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

binary to ascii

Started by hfheatherfox07, December 21, 2012, 03:44:57 PM

Previous topic - Next topic

hfheatherfox07

Hi,
I wanted to do a binary to ASCII conversion and I found this snippet
what kind of asm is this ???

Thank you!
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

anta40


hfheatherfox07

Thanks
I got an ASCII to Binary but not Binary To ASCII
I guess I better see if I can convert it to MASM some how lol
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

jj2007

If it's for fun, sit down and reflect, then roll your own.
If it's for getting a job done, look into \masm32\help\masmlib.chm ;)

Farabi

Have a look at MASMLib

bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD


http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

hfheatherfox07

Quote from: Farabi on December 21, 2012, 06:12:09 PM
Have a look at MASMLib

bin2hex proc lpString:DWORD,lnString:DWORD,lpbuffer:DWORD

I was Going to do bintohex than hextoascii but I get an error with bin2hex:

include \masm32\include\masm32rt.inc

.data
bindata  db "01001000 01100101 01101100 01101100 01101111"
hstr     db 60 dup(0)
HEX_PRE  db "%X", 32, 0 ; prefix hex, 32 for space, 0 to end
mybuffer db 4096 dup(?) ; total converted string
szCapt   db "Binary Hello in Hex", 0
.code

start:

invoke bin2hex,ADDR bindata,SIZEOF bindata,ADDR hstr
invoke lstrcat, ADDR mybuffer, ADDR hstr
invoke MessageBox,NULL,addr mybuffer,addr szCapt,MB_OK
; The result should be 48 65 6C 6C 6F
Invoke ExitProcess,0

end start


ASCII hello = Binary  01001000 01100101 01101100 01101100 01101111
Should give 48 65 6C 6C 6F in Hex
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

Farabi

What do you mean by binary? An ASCII Binary or a raw binary format?
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

hfheatherfox07

I will Be inputting this into the edit box  01001000 01100101 01101100 01101100 01101111
So ASCII Binary
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

jj2007

You need two conversions, the first being bin2byte_ex. However, I couldn't get it to work - Hutch will show you an example when it's daytime down under.

If it's urgent - this one works: 48 65 6C 6C 6F

include \masm32\MasmBasic\MasmBasic.inc        ; download
.data
bindata        db "01001000 01100101 01101100 01101100 01101111"
        Init
        mov esi, offset bindata
@@:
        void Val(Cat$(Left$(esi, 8 )+"b"))
        cmp edx, -127        ; -127=flag conversion failed
        je @F
        add esi, 9
        Print Hex$(al), " "    ; or invoke lstrcat, offset buffer, Hex$(al)
        jmp @B
@@:
        Inkey "ok?"
        Exit
end start

hfheatherfox07

I found this TASM Bin2ASCII proc
But It is crashing ....
.386
.model flat,stdcall
option casemap:none

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

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


Bin2ASCII proto
.data

bindata  db "01001000 01100101 01101100 01101100 01101111"
hstr     db 60 dup(0)
HEX_PRE  db "%X", 32, 0 ; prefix hex, 32 for space, 0 to end
mybuffer db 4096 dup(?) ; total converted string
szCapt   db "Binry To ASCII", 0

.data

.code

start:
push offset bindata
call Bin2ASCII

;invoke lstrcat, ADDR mybuffer, ADDR hstr
invoke MessageBox,NULL,addr mybuffer,addr szCapt,MB_OK

Invoke ExitProcess,0
Bin2ASCII   proc   
    mov     bx, 10 ; base ss. 10 for decimal, etc.
oi2:
    xor     dx,dx
    div     bx
; Divide the number by the base ss. In the remainder of the latter figure is obtained.
; mmediately display it impossible, so keep it in the stack.
    push    dx
    inc     cx
; And A with the private repeat the same thing, separated from it by another
; figure on the right, as there will be zero, which means that more
; left only zeros.
    test    ax, ax
    jnz     oi2
; ?Now, on to a conclusion.
    mov     ah, 02h
oi3:
    pop     dx
; Retrieve the next number, translate it into a symbol and display.
    add     dl, '0'
    int     21h
; Repeat as many times as numbers counted.
    loop    oi3
    ret
Bin2ASCII   endp
end start


Original Tasm  http://www.cyberforum.ru/assembler/thread720808-page2.html

;DOS, TASM, COM-????
;tasm.exe /m
;tlink.exe /t /x
;
.model tiny
.code
.386
org 100h
start:
    mov ah,10   ;???? ??????
    lea dx,str
    mov di,dx
    int 21h

    mov ah,2    ;??????? ??????
    mov dl,10
    int 21h

    inc di      ;?? ????? ????????? ????????

    xor ax,ax   ;??????? ????????? AH-??????? ????? ?????
    xor bx,bx

    mov bl,[di] ;????? ????????? ????????
    inc di      ;?? ????????? ??????
    mov [di+bx],byte ptr ' ' ;? ????? ?????? ???????? ??????
             ;??? ???? ???-?? ??? ????? ???? ? ?????????
    inc [len]   ;????????? ????? ?????? ? ?????? ?????? ???????

m1:
    mov cl,[len]    ;
    test cl,cl  ;???? 0, ?????
    jz okString

;????? ???????
    call Probel

;?????????? ????? ???????? ? AH ? ????? ?????? ????? ????? ? BP
    cmp cl,ah
    jle m1
    mov ah,cl
    mov bp,dx
    jmp short m1    ;?? ????????? ?????

okString:
    test ah,ah  ;?? 0 - ???? ?????
    jnz okWord

    ret     ;????? ?? ?????????

okWord:
    call Schet

;????? ?? ?????
    call Bin2ASCII

    xor ax,ax   ;????? ??????? ???????
    int 16h

    ret     ;????? ?? ?????????
;----------?????????----------------
Bin2ASCII   proc    near
    mov     bx, 10 ; ????????? ??. 10 ??? ???????????? ? ?.?.
oi2:
    xor     dx,dx
    div     bx
; ????? ????? ?? ????????? ??. ? ??????? ?????????? ????????? ?????.
; ????? ???????? ?? ??????, ??????? ???????? ?? ? ?????.
    push    dx
    inc     cx
; ? ? ??????? ????????? ?? ?? ?????, ??????? ?? ???? ?????????
; ????? ??????, ???? ?? ????????? ????, ??? ??????, ??? ??????
; ????? ?????? ????.
    test    ax, ax
    jnz     oi2
; ?????? ????????? ? ??????.
    mov     ah, 02h
oi3:
    pop     dx
; ????????? ????????? ?????, ????????? ?? ? ?????? ? ???????.
    add     dl, '0'
    int     21h
; ???????? ????? ??????? ???, ??????? ???? ?????????.
    loop    oi3
    ret
Bin2ASCII   endp
;------------------------
Schet   proc    near
    mov cl,ah
    xor ax,ax
    mov di,bp
m3:
    cmp [di],byte ptr 'c'
    jnz m4
    inc al
m4:
    inc di
    loop m3
    ret
Schet   endp
;------------------------
Probel  proc    near
    mov dx,di   ;??? ?????????? ????? ????? DX-?????? ?????
    mov al,' '
    repne scasb ;

    mov cx,di   ;DI-????? ?????
    sub cx,dx   ;DX-?????? ?????. DI-DX=CX=????? ?????
    sub [len],cl    ;????? ????? ??????
    ret
Probel  endp
;--------??????-----------
str db 100
len db ?

    end start


Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

dedndave

that's not binary, it's an "ASCII binary string (with spaces)"   :P
i doubt there is a routine for that in the masm32 library because it's kind of a "special" case

the example you show has 5 bytes of data
if you limit it to 8 bytes, it would be pretty simple, because it would fit into an unsigned dword
to strip out spaces, you might just skip over anything that isn't an ASCII "0" or an ASCII "1"
they could be commas, periods, spaces, hyphens, etc
and, of course, terminate at a null byte
it would be a little harder if you wanted to verify the "8 bits then space" format

speed probably isn't critical, because you aren't going to convert 100,000 of these strings - just 1

dedndave

the expected format is "01001000 01100101 01101100 01101100 01101111"
how do you want the routine to respond if they enter "1001000 1100101 1101100 1101100 1101111" ?
or "1 1 1 1 1     1 1" ?
or "101010101010101010101010" ?

how is this thing going to be used ?
you need to define a set of rules   :P

jj2007

Quote from: dedndave on December 21, 2012, 08:24:46 PM
how do you want the routine to respond if they enter "1001000 1100101 1101100 1101100 1101111" ?
or "1 1 1 1 1     1 1" ?
or "101010101010101010101010" ?

That can be tricky indeed.

include \masm32\MasmBasic\MasmBasic.inc        ; download
.data
bindata        db "1000 101 1101100 01101100 11   111 0  11 10000 10101010 11 11 000000000"

        Init
        mov esi, offset bindata        ; or mov esi, Win$(hEdit)
@@:
        void Val(Cat$(Left$(esi, Instr_(esi, " ")-1)+"b"))
        cmp edx, -127        ; -127=flag conversion failed
        je @F
        movzx edx, dl        ; get bytes used
        add esi, edx          ; advance source pointer
        Print Hex$(al), " "        ; or invoke lstrcat, offset buffer, Hex$(al)
        .Repeat
                lodsb
        .Until al>="0" || !al
        dec esi
        test al, al
        jne @B
@@:
        Inkey "ok?"
        Exit
end start

Output:
08 05 6C 6C 03 07 00 03 10 AA 03 03 00

hfheatherfox07

Quote from: dedndave on December 21, 2012, 08:24:46 PM
how is this thing going to be used ?
you need to define a set of rules   :P


Ya you opened my eyes dedndave .....
I got a routine that converts ASCII strings into Binary ASCII
as I posted .....
I guess all this came from seeing posts with people's signatures in ASCII binary , I guess to be cool and be like Neo from matrix , just wanted to make a little converter to allow me to read what they wrote....
So the user will paste the ASCII as I posted it ...
But it is a good question , what will happen if They get that ASCII binary off the net
And it is spaced differently .....
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

dedndave

i think i would be inclined to pre-parse the string, then do a conversion loop
one case that needs clarification is if there is a portion of the string that has more than 8 bin digits

but, you might parse through the string....
first, push a 0 onto the stack
find the address of the beginning of each series of "1"s and "0"s and push it on the stack
let the conversion routine identify the end of each series
pop the addresses off the stack and call the routine
when you pop a 0, you are done
reverse the bytes and convert it to a big binary value   :biggrin:

oh, ok - you want to convert them to ASCII bytes - that's a little different