Android Question Parse String with hex data frame

michw

Member
Licensed User
Longtime User
Hello,
is the simple way to convert string with hex data to array int data?
I have string like:

example:
Dim s as string
s="12FF238511"

I want put:

Example:
Dim B() as int

b(0)=0x12
b(1)=0xFF

b(2)=0x23
b(3)=0x85
b(4)=0x11

How can i do it?
 

agraham

Expert
Licensed User
Longtime User
ByteConverter library has a HexToBytes method that will give you an array of Byes that you could then convert to Ints if you really need Ints, remembering that byte values are -128 to 127 so you would need to mask them if you want values from 0 to 255
B4X:
    Dim BC As ByteConverter
    Dim s As String = "12FF238511"
    Dim ByteArray(0) As Byte = BC.HexToBytes(s)
    Dim IntArray(ByteArray.Length) As Int
    For i = 0 To ByteArray.Length - 1
        IntArray(i) = Bit.And(ByteArray(i), 0xff)
    Next
 
Upvote 2

michw

Member
Licensed User
Longtime User
ByteConverter library has a HexToBytes method that will give you an array of Byes that you could then convert to Ints if you really need Ints, remembering that byte values are -128 to 127 so you would need to mask them if you want values from 0 to 255
B4X:
    Dim BC As ByteConverter
    Dim s As String = "12FF238511"
    Dim ByteArray(0) As Byte = BC.HexToBytes(s)
    Dim IntArray(ByteArray.Length) As Int
    For i = 0 To ByteArray.Length - 1
        IntArray(i) = Bit.And(ByteArray(i), 0xff)
    Next

Thanks, it works perfect! I was doing something similar but a little wrong.
Thank you!
 
Upvote 0

Similar Threads

Top