B4R Question StringToUnsignedInt (missing ArrayCopy method in Bit object)

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I have used the below function by Erel for some time in B4J from another case:
B4X:
Sub StringToUnsignedInt (Str As String) As Long
    Dim converter As ByteConverter
    Dim a(8) As Byte
    Dim i() As Byte = converter.HexToBytes(Str)
    Bit.ArrayCopy(i, 0, a, 4, 4)
    Dim ll() As Long = converter.LongsFromBytes(a)
    Return ll(0)
End Sub

B4r bit object does not have the "ArrayCopy" method so it won't build. Any workaround?
thanks!
 

giggetto71

Active Member
Licensed User
Longtime User
The input is a string like "0x1562AFD" and the output should be its long equivalent. Maybe there is an easier way..sorry if it's a stupid request.
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
still have some issues. for some numbers the method throws NumberFormatException.
For example
Dim v_ulong As ULong = Bit.ParseInt("E0E09E61", 16)
any idea?

java.lang.NumberFormatException: For input string: "E0E09E61"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at anywheresoftware.b4a.keywords.Bit.ParseInt(Bit.java:86)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
...................
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim bc As ByteConverter
Dim b() As Byte = bc.HexToBytes("E0E09E61")
Dim l() As ULong = bc.ULongsFromBytes(b)
Log(l(0))
The input is a string like "0x1562AFD" and the output should be its long equivalent.
Not good enough. You should post the expected number. There are several possible outputs.
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
Not good enough. You should post the expected number. There are several possible outputs.

ok. I would like for example to input FF02FD and get 16712445 which is the decimal conversion of that Hex number.
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
still have some issues. for some numbers the method throws NumberFormatException.
For example
Dim v_ulong As ULong = Bit.ParseInt("E0E09E61", 16)
any idea?

java.lang.NumberFormatException: For input string: "E0E09E61"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at anywheresoftware.b4a.keywords.Bit.ParseInt(Bit.java:86)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
...................
This exception comes from B4J. With B4R you get the right results
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
This exception comes from B4J. With B4R you get the right results

true. I was not at home and did not have any ESP8266 to try to I made a quick test with B4J assuming a similar behavior..
 
Upvote 0
Top