Sign in to follow this  
Followers 0
2o2

string to binary

7 posts in this topic

#include <GUIConstants.au3>

guicreate("Binary Converter", 300, 300)

$menu = guictrlcreatemenu("Menu")
$open = guictrlcreatemenuitem("Open", $menu)
$save = guictrlcreatemenuitem("Save", $menu)
$copy = guictrlcreatemenuitem("Copy", $menu)
$paste = Guictrlcreatemenuitem("Paste", $menu)
$exit = Guictrlcreatemenuitem("Exit", $menu)
$about = Guictrlcreatemenuitem("About", $menu)
$display = Guictrlcreateedit("", 5, 60, 290, 215)
$tobinary = Guictrlcreatebutton("Convert to Binary", 5, 5, 145, 50)
$tostring = Guictrlcreatebutton("Convert to String", 150, 5, 145, 50)

guisetstate(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case ($msg = -3)
            Exit
        Case ($msg = $copy)
            $readata = guictrlread($display)
            clipput($readata)
        Case ($msg = $paste)
            $put = clipget()
            guictrlsetdata($display, $put)
        Case ($msg = $exit)
            Exit
        Case ($msg = $about)
            msgbox(0, "About", "")
        Case ($msg = $open)
            $openfile = fileopendialog("Open", @desktopdir, "All Files (*.*)")
            $openfileread = fileopen($openfile, 0)
            $data2 = fileread($openfileread)
            guictrlsetdata($display, $data2)
        Case ($msg = $save)
            $readdata = guictrlread($display)
            $savedialog = filesavedialog("Save", @desktopdir, "Text Files (*.txt)")
            filewrite($savedialog, $readdata)
        Case ($msg = $tobinary)
            $data = guictrlread($display)
            $char = string($data)
            $bin = stringtobinary($char)
            Guictrlsetdata($display, $bin)
        Case ($msg = $tostring)
            $data1 = guictrlread($display)
            $bin1 = binary($data1)
            $string = binarytostring($bin1)
            GUICtrlSetData($display, $string)
    EndSelect
WEnd

Theres my autoit script that converts string to binary and vice versa. I was wondering if anybody knew why the binary apears like this "0x6175746F697420646F65736E7420737570706F72742062696E617279" and not like this "0100101010101010100101010"??? could somebody please tell me how to get bare 1's and 0's binary??


Rick rack ree, kick 'em in the knee.Rick rack rass, kick 'em in the other knee!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <GUIConstants.au3>

guicreate("Binary Converter", 300, 300)

$menu = guictrlcreatemenu("Menu")
$open = guictrlcreatemenuitem("Open", $menu)
$save = guictrlcreatemenuitem("Save", $menu)
$copy = guictrlcreatemenuitem("Copy", $menu)
$paste = Guictrlcreatemenuitem("Paste", $menu)
$exit = Guictrlcreatemenuitem("Exit", $menu)
$about = Guictrlcreatemenuitem("About", $menu)
$display = Guictrlcreateedit("", 5, 60, 290, 215)
$tobinary = Guictrlcreatebutton("Convert to Binary", 5, 5, 145, 50)
$tostring = Guictrlcreatebutton("Convert to String", 150, 5, 145, 50)

guisetstate(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case ($msg = -3)
            Exit
        Case ($msg = $copy)
            $readata = guictrlread($display)
            clipput($readata)
        Case ($msg = $paste)
            $put = clipget()
            guictrlsetdata($display, $put)
        Case ($msg = $exit)
            Exit
        Case ($msg = $about)
            msgbox(0, "About", "")
        Case ($msg = $open)
            $openfile = fileopendialog("Open", @desktopdir, "All Files (*.*)")
            $openfileread = fileopen($openfile, 0)
            $data2 = fileread($openfileread)
            guictrlsetdata($display, $data2)
        Case ($msg = $save)
            $readdata = guictrlread($display)
            $savedialog = filesavedialog("Save", @desktopdir, "Text Files (*.txt)")
            filewrite($savedialog, $readdata)
        Case ($msg = $tobinary)
            $data = guictrlread($display)
            $char = string($data)
            $bin = stringtobinary($char)
            Guictrlsetdata($display, $bin)
        Case ($msg = $tostring)
            $data1 = guictrlread($display)
            $bin1 = binary($data1)
            $string = binarytostring($bin1)
            GUICtrlSetData($display, $string)
    EndSelect
WEnd

Theres my autoit script that converts string to binary and vice versa. I was wondering if anybody knew why the binary apears like this "0x6175746F697420646F65736E7420737570706F72742062696E617279" and not like this "0100101010101010100101010"??? could somebody please tell me how to get bare 1's and 0's binary??

0x32 means hexadeciaml 32. This equals 3 x 16 + 2. In bits it is 00110010.

So, you can convert the hex value to bits like this

$hexvalue='AB209FC'
$hexvalue = StringSplit($hexvalue,'')
$Result = ''

$bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
$bits = stringsplit($bits,'|')
for $n = 1 to $hexvalue[0]
      $result &=  $bits[Dec($hexvalue[$n])+1]
Next
 
ConsoleWrite($result)

EDIT: Replaced my first code which I hadn't tested and of course it didn't work.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

0x32 means hexadeciaml 32. This equals 3 x 16 + 2. In bits it is 00110010.

So, you can convert the hex value to bits like this

$hexvalue='AB209FC'
$hexvalue = StringSplit($hexvalue,'')
$Result = ''

$bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
$bits = stringsplit($bits,'|')
for $n = 1 to $hexvalue[0]
      $result &=  $bits[Dec($hexvalue[$n])+1]
Next
 
ConsoleWrite($result)

EDIT: Replaced my first code which I hadn't tested and of course it didn't work.

Your script confuses me. Can you please write it so that it works with my program or explain how to make it work with my program. I am not an expert at strings so please help! Thanks!!

Rick rack ree, kick 'em in the knee.Rick rack rass, kick 'em in the other knee!

Share this post


Link to post
Share on other sites

Your script confuses me. Can you please write it so that it works with my program or explain how to make it work with my program. I am not an expert at strings so please help! Thanks!!

I got it to work, but how would i convert binary back to string?????

Rick rack ree, kick 'em in the knee.Rick rack rass, kick 'em in the other knee!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I got it to work, but how would i convert binary back to string?????

All this is much more difficult in AutoIt because there is very limited handling of binary values. In most other languages I've used it's a lot easier. Of course I might be missing something obvious and in that case I will be very pleased if someone can show me.

Meanwhile, this is what I think-

Global $sHex = InputBox('Enter a hex vlaue','Use only 0123456789ABCDEF characters')

Global $bin = _HexToBinaryString($sHex)

if @error = -1 Then
    MsgBox(0,'ERROR','illegal character used')
    Exit
EndIf

consolewrite('$hexvalue ' & $sHex & ' is ' & $Bin & ' in binary.' & @LF)

ConsoleWrite($bin & ' in Hex is ' & _BinaryToHexString($bin) & @LF)



Func _HexToBinaryString($HexValue)
    Local $Allowed = '0123456789ABCDEF'
    Local $Test,$n
    Local $Result = ''
    if $hexValue = '' then
        SetError(-2)
        Return
    EndIf

    $hexvalue = StringSplit($hexvalue,'')
    for $n = 1 to $hexValue[0]
        if not StringInStr($Allowed,$hexvalue[$n]) Then
            SetError(-1)
            return 0
        EndIf
    Next

    Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
    $bits = stringsplit($bits,'|')
    for $n = 1 to $hexvalue[0]
        $Result &=  $bits[Dec($hexvalue[$n])+1]
    Next

    Return $Result

EndFunc

Func _BinaryToHexString($BinaryValue)
    Local $test, $Result = '',$numbytes,$nb

    if $BinaryValue = '' Then
        SetError(-2)
        Return
    endif

    Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
    $bits = stringsplit($bits,'|')
    #region check string is binary
    
    $test = stringreplace($BinaryValue,'1','')
    $test = stringreplace($test,'0','')
    if $test <> '' Then
        SetError(-1);non binary character detected
        Return
    endif
    #endregion check string is binary

    #region make binary string an integral multiple of 4 characters
    While 1
        $nb = Mod(StringLen($BinaryValue),4)
        if $nb = 0 then exitloop
        $BinaryValue = '0' & $BinaryValue
    WEnd
    #endregion make binary string an integral multiple of 4 characters

    $numbytes = Int(StringLen($BinaryValue)/4);the number of bytes

    Dim $bytes[$numbytes],$Deci[$numbytes]
    For $j = 0 to $numbytes - 1;for each byte
;extract the next byte
        $bytes[$j] = StringMid($BinaryValue,1+4*$j,4)

;find what the dec value of the byte is
        for $k = 0 to 15;for all the 16 possible hex values
            if $bytes[$j] = $bits[$k+1] Then
                $Deci[$j] = $k
                ExitLoop
            EndIf
        next
    Next

;now we have the decimal value for each byte, so stitch the string together again
    $Result = ''
    for $l = 0 to $numbytes - 1
        $Result &= Hex($Deci[$l],1)
    Next
    return $Result
EndFunc

EDIT:Changed the name of a variable to make it less confusing.

EDIT2; There's a better solution here by crzftx.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I now have this. It kinda works half the time and half the time it doesnt. it makes s and 0 the same binary value. How do i fix this??

#include <GUIConstants.au3>

guicreate("Binary Converter", 300, 300)

$menu = guictrlcreatemenu("Menu")
$open = guictrlcreatemenuitem("Open", $menu)
$save = guictrlcreatemenuitem("Save", $menu)
$copy = guictrlcreatemenuitem("Copy", $menu)
$paste = Guictrlcreatemenuitem("Paste", $menu)
$exit = Guictrlcreatemenuitem("Exit", $menu)
$about = Guictrlcreatemenuitem("About", $menu)
$display = Guictrlcreateedit("", 5, 60, 290, 215)
$tobinary = Guictrlcreatebutton("Convert to Binary", 5, 5, 145, 50)
$tostring = Guictrlcreatebutton("Convert to String", 150, 5, 145, 50)

guisetstate(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case ($msg = -3)
            Exit
        Case ($msg = $copy)
            $readata = guictrlread($display)
            clipput($readata)
        Case ($msg = $paste)
            $put = clipget()
            guictrlsetdata($display, $put)
        Case ($msg = $exit)
            Exit
        Case ($msg = $about)
            msgbox(0, "About", "")
        Case ($msg = $open)
            $openfile = fileopendialog("Open", @desktopdir, "All Files (*.*)")
            $openfileread = fileopen($openfile, 0)
            $data2 = fileread($openfileread)
            guictrlsetdata($display, $data2)
        Case ($msg = $save)
            $readdata = guictrlread($display)
            $savedialog = filesavedialog("Save", @desktopdir, "Text Files (*.txt)")
            filewrite($savedialog, $readdata)
        Case ($msg = $tobinary)
            $input = guictrlread($display)
            $hexvalue = StringSplit($input,'')
            $Result = ''
            $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
            $bits = stringsplit($bits,'|')
        for $n = 1 to $hexvalue[0]
            $result &=  $bits[Dec($hexvalue[$n])+1]
            
        Next
            Guictrlsetdata($display, $result)
        ;$data = guictrlread($display)
        ;$char = string($data)
        ;$bin = stringtobinary($char)
        ;Guictrlsetdata($display, $bin)
        Case ($msg = $tostring)
            $binaryvalue = guictrlread($display)
        Local $test, $Result = '',$numbytes,$nb

        if $BinaryValue = '' Then
            SetError(-2)
            Return
        endif
        Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
            $bits = stringsplit($bits,'|')
            #region check string is binary
            $test = stringreplace($BinaryValue,'1','')
            $test = stringreplace($test,'0','')
        if $test <> '' Then
            SetError(-1);non binary character detected
            Return
        endif
            #endregion check string is binary
            #region make binary string an integral multiple of 4 characters
    While 1
            $nb = Mod(StringLen($BinaryValue),4)
            if $nb = 0 then exitloop
            $BinaryValue = '0' & $BinaryValue
    WEnd
    #endregion make binary string an integral multiple of 4 characters

            $numbytes = Int(StringLen($BinaryValue)/4);the number of bytes

            Dim $bytes[$numbytes],$Deci[$numbytes]
            For $j = 0 to $numbytes - 1;for each byte
        ;extract the next byte
            $bytes[$j] = StringMid($BinaryValue,1+4*$j,4)

        ;find what the dec value of the byte is
        for $k = 0 to 15;for all the 16 possible hex values
            if $bytes[$j] = $bits[$k+1] Then
            $Deci[$j] = $k
            ExitLoop
            EndIf
        next
    Next

;now we have the decimal value for each byte, so stitch the string together again
    $Result = ''
    for $l = 0 to $numbytes - 1
        $Result &= Hex($Deci[$l],1)
    Next
    guictrlsetdata($display, $result)
        ;$data1 = guictrlread($display)
        ;$bin1 = binary($data1)
        ;$string = binarytostring($data1)
        ;GUICtrlSetData($display, $string)
    EndSelect
WEnd

Rick rack ree, kick 'em in the knee.Rick rack rass, kick 'em in the other knee!

Share this post


Link to post
Share on other sites

Maybe this will do it.

#include <GUIConstants.au3>

guicreate("Binary Converter", 300, 300)

$menu = guictrlcreatemenu("Menu")
$open = guictrlcreatemenuitem("Open", $menu)
$save = guictrlcreatemenuitem("Save", $menu)
$copy = guictrlcreatemenuitem("Copy", $menu)
$paste = Guictrlcreatemenuitem("Paste", $menu)
$exit = Guictrlcreatemenuitem("Exit", $menu)
$about = Guictrlcreatemenuitem("About", $menu)
$display = Guictrlcreateedit("", 5, 60, 290, 215)
$tobinary = Guictrlcreatebutton("Convert to Binary", 5, 5, 145, 50)
$tostring = Guictrlcreatebutton("Convert to String", 150, 5, 145, 50)

guisetstate(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case ($msg = -3)
            Exit
        Case ($msg = $copy)
            $readata = guictrlread($display)
            clipput($readata)
        Case ($msg = $paste)
            $put = clipget()
            guictrlsetdata($display, $put)
        Case ($msg = $exit)
            Exit
        Case ($msg = $about)
            msgbox(0, "About", "")
        Case ($msg = $open)
            $openfile = fileopendialog("Open", @desktopdir, "All Files (*.*)")
            $openfileread = fileopen($openfile, 0)
            $data2 = fileread($openfileread)
            guictrlsetdata($display, $data2)
        Case ($msg = $save)
            $readdata = guictrlread($display)
            $savedialog = filesavedialog("Save", @desktopdir, "Text Files (*.txt)")
            filewrite($savedialog, $readdata)
        Case ($msg = $tobinary)
            $input = guictrlread($display)
            $res = _HexToBinaryString($input)
            switch @error
                Case 0
                    Guictrlsetdata($display, $res)
                Case -1
                    MsgBox(0,'ERROR',$input & ' is not a valid Hex value.')
                case -2
                    Guictrlsetdata($display, '')
            EndSwitch
        Case ($msg = $tostring)
            $binaryvalue = guictrlread($display)
            $res = _BinaryToHexString($BinaryValue)
            switch @error
                Case 0
                    Guictrlsetdata($display, $res)
                Case -1
                    MsgBox(0,'ERROR',$input & ' is not a valid Binary value.')
                case -2
                    Guictrlsetdata($display, '')
            EndSwitch
    EndSelect
WEnd



Func _BinaryToHexString($BinaryValue)
    Local $test, $Result = '',$numbytes,$nb

    if $BinaryValue = '' Then
        SetError(-2)
        Return
    endif

    Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
    $bits = stringsplit($bits,'|')
    #region check string is binary

    $test = stringreplace($BinaryValue,'1','')
    $test = stringreplace($test,'0','')
    if $test <> '' Then
        SetError(-1);non binary character detected
        Return
    endif
    #endregion check string is binary

    #region make binary string an integral multiple of 4 characters
    While 1
        $nb = Mod(StringLen($BinaryValue),4)
        if $nb = 0 then exitloop
        $BinaryValue = '0' & $BinaryValue
    WEnd
    #endregion make binary string an integral multiple of 4 characters

    $numbytes = Int(StringLen($BinaryValue)/4);the number of bytes

    Dim $bytes[$numbytes],$Deci[$numbytes]
    For $j = 0 to $numbytes - 1;for each byte
    ;extract the next byte
        $bytes[$j] = StringMid($BinaryValue,1+4*$j,4)

    ;find what the dec value of the byte is
        for $k = 0 to 15;for all the 16 possible hex values
            if $bytes[$j] = $bits[$k+1] Then
                $Deci[$j] = $k
                ExitLoop
            EndIf
        next
    Next

;now we have the decimal value for each byte, so stitch the string together again
    $Result = ''
    for $l = 0 to $numbytes - 1
        $Result &= Hex($Deci[$l],1)
    Next
    return $Result
EndFunc



Func _HexToBinaryString($HexValue)
    Local $Allowed = '0123456789ABCDEF'
    Local $Test,$n
    Local $Result = ''
    if $hexValue = '' then
        SetError(-2)
        Return
    EndIf

    $hexvalue = StringSplit($hexvalue,'')
    for $n = 1 to $hexValue[0]
        if not StringInStr($Allowed,$hexvalue[$n]) Then
            SetError(-1)
            return 0
        EndIf
    Next

    Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
    $bits = stringsplit($bits,'|')
    for $n = 1 to $hexvalue[0]
        $Result &=  $bits[Dec($hexvalue[$n])+1]
    Next

    Return $Result

EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
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
Sign in to follow this  
Followers 0