Sign in to follow this  
Followers 0
2o2

Binary

7 posts in this topic

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?? If you can, could you please edit my script and make it work with the binary! thanks so much!! Martin can help me b/c hes amazing!! Ty Martin!!

#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

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



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?? If you can, could you please edit my script and make it work with the binary! thanks so much!! Martin can help me b/c hes amazing!! Ty Martin!!

#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
Look at you thread in example scripts.

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

Look at you thread in example scripts.

It confuses me. How would i get it to work in my script??

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

It confuses me. How would i get it to work in my script??

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)

Math.... my strong subject

okay, so you have six apples, and you want the binary form.

You could count them: [implied 0], 1, 10, 11, 100, 101, 110

Or you could group them, we can tell there isn't 1000, but there is definitely 100, so we subtract that amount away, we can now see a group of 10, and there are none left. 100 + 10 = 110.

The second is much more efficient for a program. (There may be a faster way, I would be interested to see one.) Unfortunately, though, a program can't guesstimate whether there are 100 or 10 present. Luckily, Int(ln(6)/ln(2))+1 returns 3, the amount of digits (groups).

Creating a simple loop:

$output = 0
$input = 6  ;you set this (in base 10)
$base = 2  ;you set this too (less than 11)
$digits = Int(Log($input)/Log($base))
For $z = $digits to 0 step -1
  $output = $output & String(Int($input / $base^$z))
  $input -= Int($input / $base^$z)*$base^$z
NextoÝ÷ Ù8Z¶ÈhºWp¢¹¢·(÷«¶)àj|jǵÒ)í¡¶¬{Múm«N(÷«µ¶¬{Múm«{hm«×K(ëax+mçb}÷«z{b²wz˯&®¶­sbb33c¶÷WGWBÒ¢b33c¶çWBÒgV÷C³gV÷C²·÷R6WBF0¢b33c¶çWD&6RÒ"·F22Ç6òæV6W76'Fò6WBÆW72Fâ¥vÆRb33c¶çWBfwC²gV÷C²gV÷C°¢b33c¶÷WGWB³Ò7G&ætÆVgBb33c¶çWBâb33c¶çWD&6Uâ7G&ætÆVâb33c¶çWBÓ¢b33c¶çWBÒ7G&æuG&ÔÆVgBb33c¶çWBÃ¥tVæ@

EDIT: edit erased everything inside the tags, had to rewrite the functions.

Edited by crzftx

Share this post


Link to post
Share on other sites

Does this convert binary to string?? im confuzed? I have this converting string to binary but what should i do to convert this back to string?? And if the code that you wrote does, tell me how to apply that into my program plz!

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]

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

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

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