Jump to content

Perfect Binary to String Converter!


2o2
 Share

Recommended Posts

I finally got this script to work!!! thanks a lot Martin and Crazfx and the guy who told me i could include <string.au3> and use _hextostring and _stringtohex!! Here is the script. Thanks Guys!!

#include <GUIConstants.au3>
#include <string.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)
            $input5 = guictrlread($display)
            $input4 = _StringToHex($input5)
            $res = _HexToBinaryString($input4)
            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)
            $res1 = _hextostring($res)
            switch @error
                Case 0
                    Guictrlsetdata($display, $res1)
                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

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

Link to comment
Share on other sites

Were you born a total moron or do you have to work at it?

Why did you say that?
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.
Link to comment
Share on other sites

Bit harsh, isn't it?

Maybe.

Why did you say that?

5 separate threads on the same topic in 2 days.

http://www.autoitscript.com/forum/index.ph...st&p=452568

http://www.autoitscript.com/forum/index.ph...st&p=452549

http://www.autoitscript.com/forum/index.ph...st&p=452927

http://www.autoitscript.com/forum/index.ph...st&p=452987

including this one after being questioned in the previous one about how many times he would be starting new threads on the same topic.

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

Maybe.

5 separate threads on the same topic in 2 days.

http://www.autoitscript.com/forum/index.ph...st&p=452568

http://www.autoitscript.com/forum/index.ph...st&p=452549

http://www.autoitscript.com/forum/index.ph...st&p=452927

http://www.autoitscript.com/forum/index.ph...st&p=452987

including this one after being questioned in the previous one about how many times he would be starting new threads on the same topic.

In that case I think it would have been better just to say that, it's a reasonable criticism.

Moron (psychology), a psychology-related term for a person with a genetically determined mental age between 8 and 12.

So if someone is born a moron, ie with a mental age of between 8 and 12 they have to be an incredible genius, and of course being a genius can take hard work so "Were you born a total moron or do you have to work at it?" was obviously a huge compliment.

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.
Link to comment
Share on other sites

In that case I think it would have been better just to say that, it's a reasonable criticism.

Moron (psychology), a psychology-related term for a person with a genetically determined mental age between 8 and 12.

So if someone is born a moron, ie with a mental age of between 8 and 12 they have to be an incredible genius, and of course being a genius can take hard work so "Were you born a total moron or do you have to work at it?" was obviously a huge compliment.

Lol ... Excellent martin !

Link to comment
Share on other sites

mo·ron noun

1. a person who is notably stupid or lacking in good judgment.

2. Psychology - a person of borderline intelligence in a former classification of mental retardation, having an intelligence quotient of 50 to 69.

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

Were you born a total moron or do you have to work at it?

Naw... I had to work at it because I don't spend my whole life making autoit scripts... get a life, get laid, seriously...

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

Link to comment
Share on other sites

  • Developers

I think this is enough.

Please just refrain from comments when you don't have anything to contribute....

Merry Christmas to all

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

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