# String To Binary.au3

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

##### 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?!?!

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

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

Somewhere Out ThereJames Ingram

Your virtual HD wherever you go, anywhere!

## Create an account

Register a new account

×

• Wiki

• Back

• Git