Jump to content

problem calculating ASCII to binary


Recommended Posts

well... here is the part of the code that converts the string into a binary(or should)

If $m=$bBIN Then
        $string=GUICtrlRead ($Input)
        $string=StringToBinary ($string)
        GUICtrlSetData ($Output, $string)       
        $String=''
            $a=StringSplit ($a, '')
            For $i=1 To $a[0]
                $Asc=Asc ($a[$i])
                If $Asc<>32 Then
                    $oBin=''
                    $x=$Asc
                    For $iii=7 To 0 Step -1
                        If $x>=2^$iii Then
                            $x-=2^$iii
                            $oBin&=1
                        Else
                            $oBin&=0
                        EndIf
                        GUICtrlSetData ($Output, GUICtrlRead ($Output) & ' ' & $oBin)
                    Next
                Else
                    GUICtrlSetData ($Output, GUICtrlRead ($Output) & ' ')
                EndIf
            Next
    EndIf

lol that is the [bIN IT] Button....

but yeah as you can see.. the bottom box (binary) IS NOT!! what the top box is....

Posted Image

Link to comment
Share on other sites

well.. im splitting up the string in the top editbox and getting the ASC() of its chr for EACH chr... and if its a space (32) then it puts a ' 'instead of a '0' or a '1'... BUT the problem is its giving me roughly 17 more binary values than i need... and none of them are 'B'.... so... doing binary conversions on paper i find is much easyer than learning how to make your own converter... lol but i gotta hand this project into my teacher so i can't give up on it...

any ideas on why its giving me this erratic value?.... and yeah... why there is a HEX at the beggining.. any ideas on whats wrong with my above code?

^_^

Link to comment
Share on other sites

What is the content of $a before you split it?

Anyway, take an example:

#include <String.au3>

Dim $sStr = 'Bobcat'
Dim $sBin = _StrtoBin($sStr)
ConsoleWrite($sBin & @LF)


Func _StrtoBin($sString)
    Local $iChr, $i
    Local $sTmp, $sRet = ''
    
    For $i = 1 To BinaryLen($sString)
        $iChr = Int(BinaryMid($sString, $i, 1))
        $sTmp = ''
        
        While $iChr > 0
            $sTmp &= Mod($iChr, 2)
            $iChr = Int($iChr/2)
        WEnd
        $sRet &= StringFormat('%08s', _StringReverse($sTmp)) & ' '
        
    Next
    
    Return StringTrimRight($sRet, 1)
EndFunc

Edit: StringFormat. ^_^

Edited by Authenticity
Link to comment
Share on other sites

hmmmm il take a look at that... most of those are new to me... like

BinaryLen
BinaryMid
StringFormat
Int
Mod

haha... time to learn... well.. i might know int and mod

int is... the same as Number()? right except it returns a whole number and not a decimal value...

mod is... a dividing operation.. eh... HELP FILE IM BACK!! ^_^

Link to comment
Share on other sites

well.. no offence but i didn't even use those functions... i just had to move like 2 lines outside of a loop lmao... but here is the string to binary function.. now ima work on binary to string

If $m=$bBIN Then
        GUICtrlSetData ($Output, '')
        $String=''
        $a=StringSplit ($a, '') 
        For $i=1 To $a[0] 
            $Asc=Asc ($a[$i]) 
            If $Asc<>32 Then
                $oBin=''
                For $iii=7 To 0 Step -1
                    If $Asc>=2^$iii Then
                        $Asc-=2^$iii
                        $oBin&=1
                    Else
                        $oBin&=0
                    EndIf
                Next
                GUICtrlSetData ($Output, GUICtrlRead ($Output) & ' ' & $oBin)
            Else
                GUICtrlSetData ($Output, GUICtrlRead ($Output) & ' -')
            EndIf
        Next
        GUICtrlSetData ($Input, '')
        GUICtrlSetState ($bBIN, $GUI_DISABLE)
        GUICtrlSetState ($bScammble, $GUI_DISABLE)
        GUICtrlSetState ($bBINCHR, $GUI_ENABLE)
    EndIf
Link to comment
Share on other sites

Here's another working example

Global $debug = True
_StringToBinary('Test123..Testing123')

Func _StringToBinary($sWord)
    Local $sChars, $nNum, $sOutput, $sBinword
    $sChars = StringSplit($sWord,"")
    For $i = 1 To $sChars[0]
        $nNum = Asc($sChars[$i])
        For $j = 0 To 7
            If BitAND($nNum,2^$j) Then
                $sBinword  &= '1'
            Else
                $sBinword  &= '0'
            EndIf
        Next
        $sOutput &= $sBinword & ' ' 
        $sBinword = ''      
    Next
    $sOutput = StringTrimRight($sOutput,1)
    If $debug Then ConsoleWrite($sWord & ' in binary = ' & $sOutput & @CRLF)
    Return $sOutput
EndFunc

and the debug line from the console is

Test123..Testing123 in binary = 00101010 10100110 11001110 00101110 10001100 01001100 11001100 01110100 01110100 00101010 10100110 11001110 00101110 10010110 01110110 11100110 10001100 01001100 11001100

Edited by Quual
Link to comment
Share on other sites

i have this code for binary to ascii conversion... it gets the first letter correct.. but everything after that isnt

EX

i did "b" and it came out "01100010" which is right... and converting back is correct also...

but when i do this "bo" it comes out "01100010 01101111" which is also right.. but converting it back to ascii gives me this "bÑ"....

If $m=$bBINCHR Then
        $string=''
        $Asc=''
        $a=GUICtrlRead ($Output)
        $a=StringSplit($a, '-')
        For $i=1 To $a[0]
            If StringLeft ($a[$i],1)=' ' Then $a[$i]=StringTrimLeft ($a[$i], 1)
            If StringRight ($a[$i],1)=' ' Then $a[$i]=StringTrimRight($a[$i], 1)
            $b=StringSplit ($a[$i], ' ')
            For $ii=1 To $b[0]
                                $Asc=0 
                For $iii=0 To 7
                    If StringMid ($b[$ii], 8-$iii, 1)=1 Then $Asc+=2^$iii
                Next
                $l=Chr ($Asc)
                $string&=$l
                $l=''
            Next
        Next
        GUICtrlSetData ($Input ,$string)
        GUICtrlSetData ($Output , '')
        GUICtrlSetState($bHEX, $GUI_ENABLE)
        GUICtrlSetState($bBIN, $GUI_ENABLE)
        GUICtrlSetState($bScammble, $GUI_ENABLE)
        GUICtrlSetState($bBINCHR, $GUI_DISABLE)
EndIf

EDIT

fixed the above code it works now.. and the entire project is in my sig "word scrammbler"

Edited by CodyBarrett
Link to comment
Share on other sites

  • 2 years later...

This doesn't actually work right. Each set of binary is backwards.

Here's another working example

Global $debug = True
_StringToBinary('Test123..Testing123')

Func _StringToBinary($sWord)
    Local $sChars, $nNum, $sOutput, $sBinword
    $sChars = StringSplit($sWord,"")
    For $i = 1 To $sChars[0]
        $nNum = Asc($sChars[$i])
        For $j = 0 To 7
            If BitAND($nNum,2^$j) Then
                $sBinword  &= '1'
            Else
                $sBinword  &= '0'
            EndIf
        Next
        $sOutput &= $sBinword & ' ' 
        $sBinword = ''      
    Next
    $sOutput = StringTrimRight($sOutput,1)
    If $debug Then ConsoleWrite($sWord & ' in binary = ' & $sOutput & @CRLF)
    Return $sOutput
EndFunc

and the debug line from the console is

Test123..Testing123 in binary = 00101010 10100110 11001110 00101110 10001100 01001100 11001100 01110100 01110100 00101010 10100110 11001110 00101110 10010110 01110110 11100110 10001100 01001100 11001100

This:

For $j = 0 To 7

Needs to be:

For $j = 7 To 0 Step -1

Then I got it work.

Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...