Android Question Binary to String & String to Binary

ilan

Expert
Licensed User
Longtime User
hi

i was able to find a working code how to convert string to binary but how can i convert binary back to string??

this is how i convert string to binary:

B4X:
Sub Button1_Click  
    Dim txt As String = "foo"
    Dim res As String = ""
   
    For Each b As Byte In txt.GetBytes("UTF8")
        res = res & NumberFormat2(ToBinaryString(Bit.And(0xff, b)), 8, 0, 0, False)' & " "
    Next
   
    Log(res)
End Sub
 
Sub ToBinaryString(number As Int) As String
    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(number, x)
        If ii <> 0 Then    sb.Append("1") Else    sb.Append("0")
        x = Bit.UnsignedShiftRight(x, 1)
    Next
    Return sb.ToString
End Sub

and now how do i convert binary back to string?

any ideas?

thanx, ilan
 

sorex

Expert
Licensed User
Longtime User
maybe there are some built-in methods for it that I don't know but this seems to work with plain code...

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    Dim txt As String = "foo"
    Dim res As String = ""
 
    For Each b As Byte In txt.GetBytes("UTF8")
        res = res & NumberFormat2(ToBinaryString(Bit.And(0xff, b)), 8, 0, 0, False)' & " "
    Next
 
    Log(res)

    ToString(res)
End Sub

Sub ToString(bin As String) As String
 Dim v As Int
 Dim o As String
 For x=0 To bin.Length-1
     If bin.CharAt(bin.Length-1-x)="1" Then v=v+Power(2,x)
 Next
 For x=0 To bin.Length/8-1
    o=Chr(Bit.And(Bit.ShiftRight(v,x*8),255)) & o
 Next
 Log(o)
End Sub

output:
Waiting for debugger to connect...
Program started.
011001100110111101101111
foo
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
or the single loop variant...

B4X:
Sub ToString(bin As String) As String
 Dim v As Int
 Dim o As String
 For x=0 To bin.Length-1
     If bin.CharAt(bin.Length-1-x)="1" Then v=v+Power(2,x Mod 8)
    If x Mod 8=7 Then
        o=Chr(v) & o
        v=0
    End If
 Next
 Log(o)
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

thanx, i have tried it but it doesnot work always.

i tried other words and i get a weird result

using hebrew chars will not work at all.

i have a great website where every word i tried work perfect
https://cryptii.com/hex-to-text

i wonder how they are doing it
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
the second code works better but only with english chars.

this is for example my binary:

110101111001100111010110101110001101011110010011

in the website it is converted correctly: יָד

but with your code i get: יָד
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
hebrew probably uses double bytes since it ain't part of the ascii charset so using unicode.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
B4X:
Sub ToString(bin As String) As String
 Dim v As Int
 Dim data(6) 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
 Log(BytesToString(data,0,6,"UTF-8"))
End Sub

Waiting for debugger to connect...
Program started.
011001100110111101101111
יָד
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok, now we can convert that word. unfortunately using a different one will crash

 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you need to tweak the size of that data array.

this is more flexible

B4X:
Sub ToString(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
 Log(BytesToString(data,0,data.Length,"UTF-8"))
End Sub

Waiting for debugger to connect...
Program started.
011001100110111101101111
רֶאְבִּיט
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…