Android Question Int() to Byte()?

stu14t

Active Member
Licensed User
Longtime User
Looking for some guidance on How to convert an array of integers to array of bytes?

Many thanks
 
Solution
Of course, since each Int is 4 bytes and 16 Int(s) make 64 bytes.

Try
Code: Copy
    Code = Regex.Split(",",Dline)
        Dim asInt as Int
        Dim asBytes(16) As Byte
        For n = 0 To 15
            If IsNumber (Code(n)) Then
                asInt = Code(n)            'Converts Chr$ to Int
                asBytes(n) = Bit.And(asInt, 0xFF)
            End If
        Next
        iv3 = asBytes
This should create a 16 byte Byte array. Note, the code can be cleaned up a bit more, I just did not want to mess with the existing iv3 declaration (you may not need asBytes). Also, I just typed this in here, you may need to fix some errors
That's done it, Many thanks. Just had to change the for next loop to 0 to 15 but otherwise perfect
I've had a look at that but I think i'm doing something wrong. I start with a comma separated text string of numbers (234,3,149,74,114,102,1,60,68,163,73,10,190,179,241,21) and then use this code, but It's not working:

B4X: Copy
    Code = Regex.Split(",",Dline)
        
        For n = 0 To 16
            If IsNumber (Code(n)) Then
                Val(n) = Code(n)            'Converts Chr$ to Int
                iv3 = conv.IntsToBytes(Val) 'Coverts Int() to Bytes
            End If
                
        Next
 
Upvote 0
What are you expecting? What are you getting?
 
Upvote 0
Hi,
I think line no 6 should be:
B4X: Copy
iv3(n) = conv.IntsToBytes(Val(n))
Ah, close, I think the integer to byte conversion needs to happen outside of the loop
Code: Copy
    Code = Regex.Split(",",Dline)
        
        For n = 0 To 16
            If IsNumber (Code(n)) Then
                Val(n) = Code(n)            'Converts Chr$ to Int
                'iv3 = conv.IntsToBytes(Val) 'Coverts Int() to Bytes
            End If
                
        Next
        iv3 = conv.IntsToBytes(Val) 'Coverts Int() to Bytes
Also, the output may be affected by the Endian setting (https://www.b4x.com/android/help/byteconverter.html#byteconverter_littleendian)
 
Upvote 0
@OliverA Many thanks, I tried the code you suggested but It has created an array that's 64 bytes long instead 16 It looks (a sample) like this:
Not sure why it's padded it out :confused:

1653338199708.png
 
Upvote 0
64 bytes long instead 16
Of course, since each Int is 4 bytes and 16 Int(s) make 64 bytes.

Try
Code: Copy
    Code = Regex.Split(",",Dline)
        Dim asInt as Int
        Dim asBytes(16) As Byte
        For n = 0 To 16
            If IsNumber (Code(n)) Then
                asInt = Code(n)            'Converts Chr$ to Int
                asBytes(n) = Bit.And(asInt, 0xFF)
            End If
        Next
        iv3 = asBytes
This should create a 16 byte Byte array. Note, the code can be cleaned up a bit more, I just did not want to mess with the existing iv3 declaration (you may not need asBytes). Also, I just typed this in here, you may need to fix some errors
 
Upvote 0
Of course, since each Int is 4 bytes and 16 Int(s) make 64 bytes.

Try
Code: Copy
    Code = Regex.Split(",",Dline)
        Dim asInt as Int
        Dim asBytes(16) As Byte
        For n = 0 To 15
            If IsNumber (Code(n)) Then
                asInt = Code(n)            'Converts Chr$ to Int
                asBytes(n) = Bit.And(asInt, 0xFF)
            End If
        Next
        iv3 = asBytes
This should create a 16 byte Byte array. Note, the code can be cleaned up a bit more, I just did not want to mess with the existing iv3 declaration (you may not need asBytes). Also, I just typed this in here, you may need to fix some errors
That's done it, Many thanks. Just had to change the for next loop to 0 to 15 but otherwise perfect
 
Upvote 0
Solution
Back
Top