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

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
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 :(

Waiting for debugger to connect...
Program started.
copied
110101111010100011010110101101101101011110010000110101101011000011010111100100011101011010111100110101101011010011010111100110011101011110011000
Error occurred on line: 292 (Main)
java.lang.ArrayIndexOutOfBoundsException: 6
at b4j.example.main._tostring(main.java:468)
at b4j.example.main._copybinarytoclip(main.java:431)
at b4j.example.main._table_mouseclicked(main.java:662)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:91)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
 
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
Top