Jump to content

String To Binary.au3


frog78
 Share

Recommended Posts

With this func you can convert a string to true binary and reverse.

$a1 = "Test"
$a2 = String_To_Binary($a1)
$a3 = Binary_To_String($a2)
MsgBox(0, "", $a1 & @CRLF & $a2 & @CRLF & $a3)
Func String_To_Binary($String)
    Local $1 = 1, $2 = "", $3 = StringLen($String), $4, $5, $6
    While $1 <= $3
        $4 = Asc(StringMid($String, $1, 1))
        $5 = 7
        While $5 >= 0
            $6 = $4 - (2 ^ $5)
            If $6 >= 0 Then
                $4 = $6
                $2 = $2 & "1"
            Else
                $2 = $2 & "0"
            EndIf
            $5 = $5 - 1
        WEnd
        $1 = $1 + 1
    WEnd
    Return "0b" & $2
EndFunc   ;==>String_To_Binary
Func Binary_To_String($Binary)
    Local $1 = 1, $2 = "", $3 = StringLen($Binary), $4, $5, $6, $7
    If StringLeft($Binary, 2) = "0b" Then
        $Binary = StringTrimLeft($Binary, 2)
        While $1 <= $3
            $4 = StringMid($Binary, $1, 8)
            $5 = 0
            $6 = 1
            $7 = 8
            While $7 >= 1
                If StringMid($4, $7, 1) = "1" Then
                    $5 = $5 + (2 ^ ($6 - 1))
                EndIf
                $6 = $6 + 1
                $7 = $7 - 1
            WEnd
            $2 = $2 & Chr($5)
            $1 = $1 + 8
        WEnd
        Return $2
    Else
        Return
    EndIf
EndFunc   ;==>Binary_To_String
Edited by frog78
Link to comment
Share on other sites

With this func you can convert a string to true binary and reverse.

$a1 = "Test"
$a2 = String_To_Binary($a1)
$a3 = Binary_To_String($a2)
MsgBox(0, "", $a1 & @CRLF & $a2 & @CRLF & $a3)
Func String_To_Binary($String)
    Local $1 = 1, $2 = "", $3 = StringLen($String), $4, $5, $6
    While $1 <= $3
        $4 = Asc(StringMid($String, $1, 1))
        $5 = 7
        While $5 >= 0
            $6 = $4 - (2 ^ $5)
            If $6 >= 0 Then
                $4 = $6
                $2 = $2 & "1"
            Else
                $2 = $2 & "0"
            EndIf
            $5 = $5 - 1
        WEnd
        $1 = $1 + 1
    WEnd
    Return "0b" & $2
EndFunc   ;==>String_To_Binary
Func Binary_To_String($Binary)
    Local $1 = 1, $2 = "", $3 = StringLen($Binary), $4, $5, $6, $7
    If StringLeft($Binary, 2) = "0b" Then
        $Binary = StringTrimLeft($Binary, 2)
        While $1 <= $3
            $4 = StringMid($Binary, $1, 8)
            $5 = 0
            $6 = 1
            $7 = 8
            While $7 >= 1
                If StringMid($4, $7, 1) = "1" Then
                    $5 = $5 + (2 ^ ($6 - 1))
                EndIf
                $6 = $6 + 1
                $7 = $7 - 1
            WEnd
            $2 = $2 & Chr($5)
            $1 = $1 + 8
        WEnd
        Return $2
    Else
        Return
    EndIf
EndFunc   ;==>Binary_To_String

(...)convert a string to true binary and reverse?!?!

And about:

StringToBinary() ; Converts a string into binary data.
BinaryToString() ; Converts a binary variant into a string.

Think about this...

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

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...