Marco Maria Vilucchi Active Member Licensed User Longtime User Aug 11, 2019 #1 Hi all, I need to convert a single byte to a string I use this code to create the byte: Code: Copy 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: Code: Copy 00010111 00010110 Now I want convert b to a string (wstr) : Code: Copy wstr = BytesToString(b, 0, 1, "UTF8") LogByte(wstr) but I have an error Code: Copy 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
Hi all, I need to convert a single byte to a string I use this code to create the byte: Code: Copy 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: Code: Copy 00010111 00010110 Now I want convert b to a string (wstr) : Code: Copy wstr = BytesToString(b, 0, 1, "UTF8") LogByte(wstr) but I have an error Code: Copy 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
Erel B4X founder Staff member Licensed User Longtime User Aug 11, 2019 #2 There is no meaning to the code you posted. Please post an example of the input and the expected output. Upvote 0
There is no meaning to the code you posted. Please post an example of the input and the expected output.
E emexes Expert Licensed User Longtime User Aug 11, 2019 #3 Presumably BytesToString expects an Array of Bytes, not just a standalone Byte. Try: Code: Copy 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") Upvote 0
Presumably BytesToString expects an Array of Bytes, not just a standalone Byte. Try: Code: Copy 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 Longtime User Aug 11, 2019 #4 sorry I try to explain myself better first of all, I repeat correct doce: Code: Copy 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: Code: Copy 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 Upvote 0
sorry I try to explain myself better first of all, I repeat correct doce: Code: Copy 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: Code: Copy 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
E emexes Expert Licensed User Longtime User Aug 11, 2019 #5 Code: Copy Dim IsThisTheAnswer As String = Chr(70) Log("IsThisTheAnswer = " & IsThisTheAnswer) gives log: Code: Copy IsThisTheAnswer = F Last edited: Aug 11, 2019 Upvote 0
Code: Copy Dim IsThisTheAnswer As String = Chr(70) Log("IsThisTheAnswer = " & IsThisTheAnswer) gives log: Code: Copy IsThisTheAnswer = F
I inakigarm Well-Known Member Licensed User Longtime User Aug 11, 2019 #6 Marco Maria Vilucchi said: sorry I try to explain myself better first of all, I repeat correct doce: Code: Copy 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: Code: Copy 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 Click to expand... Asc(70) - Asc(71) Upvote 0
Marco Maria Vilucchi said: sorry I try to explain myself better first of all, I repeat correct doce: Code: Copy 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: Code: Copy 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 Click to expand... Asc(70) - Asc(71)
E emexes Expert Licensed User Longtime User Aug 11, 2019 #7 Or if you have multiple bytes, then: Code: Copy 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: Copy BiggerAnswer = Hello Last edited: Aug 11, 2019 Upvote 0
Or if you have multiple bytes, then: Code: Copy 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: Copy BiggerAnswer = Hello
E emexes Expert Licensed User Longtime User Aug 11, 2019 #8 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. Upvote 0
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.
Marco Maria Vilucchi Active Member Licensed User Longtime User Aug 11, 2019 #9 thanks all, really.... Upvote 0
E emexes Expert Licensed User Longtime User Aug 11, 2019 #10 Still wondering where this is going, though... Upvote 0