Hi
This is a snipped how to convert String to Binary and Binary to String.
Thanks to @sorex for providing the Binary2String Code and to @Erel for the String2Binary Code.
#1, String To Binary:
Use:
#2, Binary To String:
Use:
This is a snipped how to convert String to Binary and Binary to String.
Thanks to @sorex for providing the Binary2String Code and to @Erel for the String2Binary Code.
#1, String To Binary:
B4X:
Sub String2Binary(txt As String) As String
Dim res As String = ""
For Each b As Byte In txt.GetBytes("UTF8")
Dim sb As StringBuilder
sb.Initialize
Dim x As Int = Bit.ShiftLeft(1, 31)
For i = 0 To 31
Dim ii As Int = Bit.And(Bit.And(0xff, b), x)
If ii <> 0 Then sb.Append("1") Else sb.Append("0")
x = Bit.UnsignedShiftRight(x, 1)
Next
res = res & NumberFormat2(sb.ToString, 8, 0, 0, False)' & " "
Next
Return res
End Sub
Use:
B4X:
Dim bin As String = String2Binary(txt)
#2, Binary To String:
B4X:
Sub Binary2String(Bin As String) As String
Dim v As Int
Dim data(Bin.Length/8) As Byte
For x=0 To Bin.Length-1
If Bin.CharAt(x)="1" Then v=v+Power(2,(Bin.Length-x-1) Mod 8)
If x Mod 8=7 Then
data(Floor(x/8))=v
v=0
End If
Next
Return BytesToString(data,0,data.Length,"UTF-8")
End Sub
Use:
B4X:
Log(Binary2String(bin))