1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question convert a single byte to string

Discussion in 'Android Questions' started by Marco Maria Vilucchi, Aug 11, 2019.

  1. Marco Maria Vilucchi

    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:

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

    The log is this:
    Code:
    00010111
    00010110
    Now I want convert b to a string (wstr) :
    Code:
    wstr = BytesToString(b, 01"UTF8")
        LogByte(wstr)
    but I have an error
    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    There is no meaning to the code you posted. Please post an example of the input and the expected output.
     
  3. emexes

    emexes Well-Known Member Licensed User

    Presumably BytesToString expects an Array of Bytes, not just a standalone Byte. Try:
    Code:
    Dim BA() As Byte = Array As Byte(b)    'or = Array As Byte(b, b2, b3, b4...) for multiple bytes
    wstr = BytesToString(BA, 01"UTF8")
     
    Shelby likes this.
  4. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    sorry I try to explain myself better
    first of all, I repeat correct doce:

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

    Sub LogByte(b As Byte)
        
    Log(NumberFormat2(Bit.ToBinaryString(Bit.And(0xff, b)), 800False))
    End Sub
    The log is:
    Code:
    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
     
  5. emexes

    emexes Well-Known Member Licensed User

    Code:
    Dim IsThisTheAnswer As String = Chr(70)
    Log("IsThisTheAnswer = " & IsThisTheAnswer)
    gives log:
    Code:
    IsThisTheAnswer = F
     
    Last edited: Aug 11, 2019
    Shelby and José J. Aguilar like this.
  6. inakigarm

    inakigarm Well-Known Member Licensed User

    Asc(70) - Asc(71)
     
  7. emexes

    emexes Well-Known Member Licensed User

    Or if you have multiple bytes, then:
    Code:
    Dim BiggerAnswer As String = Chr(64 + 8) & Chr(96 + 5) & Chr(96 + 12) & Chr(96 + 12) & Chr(96 + 15)
    Log("BiggerAnswer = " & BiggerAnswer)
    gives log:
    Code:
    BiggerAnswer = Hello
     
    Last edited: Aug 11, 2019
    Shelby likes this.
  8. emexes

    emexes Well-Known Member 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.
     
    Shelby likes this.
  9. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    thanks all, really....
     
  10. emexes

    emexes Well-Known Member Licensed User

    Still wondering where this is going, though... :)
     
    Shelby likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice