Android Question convert a single byte to string

Marco Maria Vilucchi

Active Member
Licensed User
Hi all,
I need to convert a single byte to a string
I use this code to create the byte:

B4X:
    Dim b As Byte
    Dim wstr As String
    b = 23
    LogByte(b)
    b = SetBit(b, 0, False)
    LogByte(b)
(this is an Erel code)

The log is this:
B4X:
00010111
00010110
Now I want convert b to a string (wstr) :
B4X:
    wstr = BytesToString(b, 0, 1, "UTF8")
    LogByte(wstr)
but I have an error
B4X:
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at com.mamavi.slimup.main._activity_create(main.java:1974)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at com.mamavi.slimup.main.afterFirstLayout(main.java:104)
    at com.mamavi.slimup.main.access$000(main.java:17)
    at com.mamavi.slimup.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
How can I solve?
Thanks in advance
 

emexes

Expert
Licensed User
Presumably BytesToString expects an Array of Bytes, not just a standalone Byte. Try:
B4X:
Dim BA() As Byte = Array As Byte(b)    'or = Array As Byte(b, b2, b3, b4...) for multiple bytes
wstr = BytesToString(BA, 0, 1, "UTF8")
 

Marco Maria Vilucchi

Active Member
Licensed User
sorry I try to explain myself better
first of all, I repeat correct doce:

B4X:
Dim b As Byte
    Dim wstr As String
    b = 71
    LogByte(b)
    b = SetBit(b, 0, False)
    LogByte(b)

Sub LogByte(b As Byte)
    Log(NumberFormat2(Bit.ToBinaryString(Bit.And(0xff, b)), 8, 0, 0, False))
End Sub
The log is:
B4X:
01000111
01000110
that is:
71
70

Now I need simply have a string with "F" (that is 70)
I hope this time I was clear
 

inakigarm

Well-Known Member
Licensed User
sorry I try to explain myself better
first of all, I repeat correct doce:

B4X:
Dim b As Byte
    Dim wstr As String
    b = 71
    LogByte(b)
    b = SetBit(b, 0, False)
    LogByte(b)

Sub LogByte(b As Byte)
    Log(NumberFormat2(Bit.ToBinaryString(Bit.And(0xff, b)), 8, 0, 0, False))
End Sub
The log is:
B4X:
01000111
01000110
that is:
71
70

Now I need simply have a string with "F" (that is 70)
I hope this time I was clear
Asc(70) - Asc(71)
 

emexes

Expert
Licensed User
Or if you have multiple bytes, then:
B4X:
Dim BiggerAnswer As String = Chr(64 + 8) & Chr(96 + 5) & Chr(96 + 12) & Chr(96 + 12) & Chr(96 + 15)
Log("BiggerAnswer = " & BiggerAnswer)
gives log:
B4X:
BiggerAnswer = Hello
 
Last edited:

emexes

Expert
Licensed User
upload_2019-8-11_21-34-58.png


The first 128 Unicode characters are the 128 ASCII characters, eg Unicode character 70 is the same as ASCII character 70, ie both are an uppercase F.
 
Top