Drago Bratko Active Member Licensed User Longtime User May 4, 2024 #1 I have simple function for converting hex string to integer. B4X: Sub TestHexToInt(Str As String) Dim converter As ByteConverter Dim b() As Byte, i() As Int b = converter.HexToBytes(Str) i = converter.IntsFromBytes(b) Log(b(0)) Log(i(0)) End Sub For test calls, for example I call this: B4X: TestHexToInt("01") TestHexToInt("55") TestHexToInt("E0") Question is why "i" array in function "TestHexToInt" is always empty?
I have simple function for converting hex string to integer. B4X: Sub TestHexToInt(Str As String) Dim converter As ByteConverter Dim b() As Byte, i() As Int b = converter.HexToBytes(Str) i = converter.IntsFromBytes(b) Log(b(0)) Log(i(0)) End Sub For test calls, for example I call this: B4X: TestHexToInt("01") TestHexToInt("55") TestHexToInt("E0") Question is why "i" array in function "TestHexToInt" is always empty?
agraham Expert Licensed User Longtime User May 4, 2024 #2 Check the length of b(). You only seem to be specifying two bytes but ByteConverter needs four bytes to create an Int as it divides the length of the byte array by 4 to know the length of the Int array to create for the conversion so you are getting a zero length i() array. As an aside I wrote I ByteConverter in the very early days of B4A (2011 according to the timestamp on my copy of the source code) and Erel adopted it as one of the official libraries together with several other of my early libraries. Upvote 0
Check the length of b(). You only seem to be specifying two bytes but ByteConverter needs four bytes to create an Int as it divides the length of the byte array by 4 to know the length of the Int array to create for the conversion so you are getting a zero length i() array. As an aside I wrote I ByteConverter in the very early days of B4A (2011 according to the timestamp on my copy of the source code) and Erel adopted it as one of the official libraries together with several other of my early libraries.
Daestrum Expert Licensed User Longtime User May 4, 2024 #3 Just add Code: Sub TestHexToInt(Str As String) If Str.Length < 8 Then Str = "00000000" & Str Str = Str.SubString(Str.Length - 8) End If ... Upvote 0
Just add Code: Sub TestHexToInt(Str As String) If Str.Length < 8 Then Str = "00000000" & Str Str = Str.SubString(Str.Length - 8) End If ...
E emexes Expert Licensed User Longtime User May 4, 2024 #4 Drago Bratko said: hex string to integer Click to expand... Is there an issue with eg Bit.ParseInt(Str, 16) ? ParseInt (Value As String, Radix As Int) As Int Parses Value as an integer using the specified radix. Radix - Should be between 2 to 36. https://www.b4x.com/b4j/help/core.html#bit_parseint Upvote 0
Drago Bratko said: hex string to integer Click to expand... Is there an issue with eg Bit.ParseInt(Str, 16) ? ParseInt (Value As String, Radix As Int) As Int Parses Value as an integer using the specified radix. Radix - Should be between 2 to 36. https://www.b4x.com/b4j/help/core.html#bit_parseint
Drago Bratko Active Member Licensed User Longtime User May 4, 2024 #5 Thanks to all. Just to give response to others who may need it, both ways (Daestrum and emexes solution) works in my case. Upvote 0
Thanks to all. Just to give response to others who may need it, both ways (Daestrum and emexes solution) works in my case.