News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

binary to ascii

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

Previous topic - Next topic

hfheatherfox07

Thanks  dedndave...still no hex

in the mean time what divides the binary string result into 7 in this attachment ?
I want to have a long string only , no spaces

http://masm32.com/board/index.php?action=dlattach;topic=1122.0;attach=959

Thank you for your time
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

Attached a console app that does conversion from Ascii to Hex and Bin and vice versa.
Usage example:

Enter empty string to exit the loop
Enter 'decode hex' or 'decode bin' to see the original strings

Text to encode = hfheatherfox07
Hex:    68 66 68 65 61 74 68 65 72 66 6F 78 30 37
Bin:    01101000 01100110 01101000 01100101 01100001 01110100 01101000 01100101
01110010 01100110 01101111 01111000 00110000 00110111

Text to encode = decode hex

Decoded from Hex=[hfheatherfox07]
Text to encode = decode bin

Decoded from Bin=[hfheatherfox07]
Text to encode =


Note you can clear the prefilled string by pressing Escape.

hfheatherfox07

How do you take out the space in between the binary string?
mov byte ptr [eax], " "   ; add a space
         inc eax

jj2007 thank you for your time  :t, but you are more advanced than me by years ..... :(
I do not get  MasmBasic
That is why for now I do examples with out it   :(
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

Quote from: hfheatherfox07 on December 22, 2012, 05:17:54 PM
How do you take out the space in between the binary string?
  mov byte ptr [eax], " "   ; add a space
  inc eax
Comment out these two, and there will be no more space. But it means you need a different mechanism to decode the 1010101001010010010101001001010101001010...

QuoteI do not get  MasmBasic
Download it...
For most projects, replacing the initial includes with include \masm32\MasmBasic\MasmBasic.inc is entirely sufficient. As a bonus, you get an incredibly useful debug macro:
deb 4, "Decoding now...", $esi, eax, ecx
Shows your current string, the contents of eax and ecx, and doesn't trash anything.
QuoteThat is why for now I do examples with out it   :(
No problem. Take my examples as pseudocode showing the steps you may need to design "by hand". Of course, some stuff looks complicated because it needs to adapt to the syntax of the available function, e.g. Val():

input: 11 3f 99 0a
Val needs: 11h ->Left$("11 3f 99 0a", 2)+"h" = 11h
void Val(Cat$(Left$(ecx, 2)+"h"))

Pretty basic stuff  :biggrin:

hfheatherfox07

I found a whole bunch of conversions here in masm32
http://www.inf.unideb.hu/~fazekasg/oktatas/comparch/mintaprogramok/

How can you convert that to work with MessageBox instead of Debug view ?
I am not familiar with Debug View
Thanks
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.

Magnum

www.programmersheaven.com/mb/x86_asm/364775/364857/re-converting-binary-into-ascii/

Posted by AsmGuru62 on 22 Aug 2007 at 3:47 AM

However, it is good only for hexadecimal dumping (fastest possible code). To do binary output - loops will be needed (unless the loop completely unrolled)


;
; This macro assumes that AL has high 4 bits set to zero
; and DI points to an ASCII buffer to dump the output
; Direction flag DF=1 (forward)
;

macro AL_DIGIT_TO_HEX
{
     CMP   AL, 10
     SBB   AL, 69h
     DAS
     STOSB
}

;
; This next macro assumes that AL is a byte to dump as ASCII
; and DI points to an ASCII buffer to dump the output
; Direction flag DF=1 (forward)
;

macro AL_TO_HEX
{
    PUSH   AX
    SHR    AL, 4
    AL_DIGIT_TO_HEX

    POP    AX
    AND    AL, 0Fh
    AL_DIGIT_TO_HEX
}

;
; This next macro assumes that AX is a word to dump as ASCII
; and DI points to an ASCII buffer to dump the output
; Direction flag DF=1 (forward)
;

macro AX_TO_HEX
{
    PUSH   AX
    SHR    AX, 8
    AL_TO_HEX

    POP    AX
    AL_TO_HEX
}

;
; Now all you have to do to convert to HEX is just
; load registers and call the macro
;

buffer db 'xxxx$'

...

    MOV     AX, 0F59Bh
    MOV     DI, buffer
    CLD
    AX_TO_HEX

    MOV     DX, buffer     ; Print it to console in DOS mode
    MOV     AH, 9h
    INT     21h
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

hfheatherfox07

@jj I am just looking to convert bin to ASCII text
I still can not figure out the masmbasic any way to do your example with out it?

I had a thought is there a way to write a look up table for each binary and the ASCII it represents ?
Would that be accurate ?
Like

A=01000001
a= 01100001


Etc....
Can anybody see a problem doing it that way?
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.

dedndave

if you look at the masm32 library code (masm32\m32lib), you'll see a few routines that use LUT's
you might get some ideas - using LUT's is quite often the fastest way to do things

however, if i want to convert a binary value to ASCII binary, i use the CRT  :P
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

        .DATA?

szOutBuf db 36 dup(?)

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  crt__itoa,12345678h,offset szOutBuf,2
        print   offset szOutBuf,13,10
        inkey
        exit

_main   ENDP

;###############################################################################################

        END     _main


jj2007

Quote from: hfheatherfox07 on January 01, 2013, 08:29:36 PM
@jj I am just looking to convert bin to ASCII text
I still can not figure out the masmbasic any way to do your example with out it?

Sure there is a way. After all, MasmBasic is assembler ;-)
Why don't you put together the logical steps and give it a try? Once you have posted a significant amount of code, you'll surely get more help :icon14:

hfheatherfox07

Quote from: jj2007 on January 01, 2013, 09:42:57 PM
Quote from: hfheatherfox07 on January 01, 2013, 08:29:36 PM
@jj I am just looking to convert bin to ASCII text
I still can not figure out the masmbasic any way to do your example with out it?

Sure there is a way. After all, MasmBasic is assembler ;-)
Why don't you put together the logical steps and give it a try? Once you have posted a significant amount of code, you'll surely get more help :icon14:

Thanks I will give it another shot ,I appreciate it
It seems shorter when you use masmbasic ....
I notice a lot of your code is really short
Once I tried to just add the functions without the lib it got large


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

here, Heather, i will get you started.....
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

Char2AscBin PROTO :DWORD,:LPVOID

;###############################################################################################

        .DATA

szString  db 'Hello Heather',0

;***********************************************************************************************

        .DATA?

szBuffer db 9 dup(?)

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        mov     esi,offset szString
        mov     edi,offset szBuffer
        jmp short Loop01

Loop00: INVOKE  Char2AscBin,eax,edi
        print   edi,32

Loop01: lodsb
        or      al,al
        jnz     Loop00

        print   chr$(13,10)
        inkey
        exit

_main   ENDP

;***********************************************************************************************

Char2AscBin PROC USES EDI dwChar:DWORD,lpBuffer:LPVOID

        mov     edx,dwChar
        mov     al,30h
        mov     edi,lpBuffer
        bswap   edx
        mov     ecx,8

ChAsc0: shr     eax,1
        shl     edx,1
        rcl     eax,1
        dec     ecx
        stosb
        jnz     ChAsc0

        xchg    eax,ecx
        stosb
        ret

Char2AscBin ENDP

;###############################################################################################

        END     _main

dedndave

that one was a console version
this one uses a MessageBox...
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

Char2AscBin PROTO :DWORD,:LPSTR
Sz2AscBin   PROTO :LPSTR,:LPSTR

;###############################################################################################

        .DATA

szString  db 'Hello Heather',0

;***********************************************************************************************

        .DATA?

szBuffer db 9*(sizeof szString-1) dup(?)

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        mov     edi,offset szBuffer
        mov     esi,offset szString
        INVOKE  Sz2AscBin,esi,edi
        INVOKE  MessageBox,HWND_DESKTOP,edi,esi,MB_OK
        exit

_main   ENDP

;***********************************************************************************************

Char2AscBin PROC USES EDI dwChar:DWORD,lpBuffer:LPSTR

        mov     edx,dwChar
        mov     al,30h
        mov     edi,lpBuffer
        bswap   edx
        mov     ecx,8

ChAsc0: shr     eax,1
        shl     edx,1
        rcl     eax,1
        dec     ecx
        stosb
        jnz     ChAsc0

        xchg    eax,ecx
        stosb
        ret

Char2AscBin ENDP

;***********************************************************************************************

Sz2AscBin PROC USES ESI EDI lpSrc:LPSTR,lpDest:LPSTR

        mov     esi,lpSrc
        mov     edi,lpDest
        jmp short SzAsc1

SzAsc0: INVOKE  Char2AscBin,eax,edi
        add     edi,8
        mov     al,32
        stosb

SzAsc1: lodsb
        or      al,al
        jnz     SzAsc0

        dec     edi
        stosb
        ret

Sz2AscBin ENDP

;###############################################################################################

        END     _main

hfheatherfox07

thanks dedndave,
I am trying to do the opposite ...I have that , I posted a tool for that a couple posts up
2nd page 6 post down http://masm32.com/board/index.php?action=dlattach;topic=1122.0;attach=959
also I was trying to take out the spaces in between the binary text out put
That is why I am am thinking  a LUT 
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

no need to "take out the spaces", per se
just skip over them as you translate
more precisely, use them to indicate a new ASCII char   :P

but - you still haven't established a set of rules for conversion
we are waiting, patiently - lol

i would say an LUT is overkill for this purpose
but - according to Hutch, size doesn't matter (probably has something to do with being single)

hfheatherfox07

I meant spaces in the result so It looks like this :
01001000011001010110110001101100011011110010000001001000011001010110000101110100011010000110010101110010

Also when using this http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_To_Text.asp
I noticed that it does not matter weather you input the above binary with the spaces or a continues string , you still get the right ASCII result!
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.