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
B4X:
    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

stu14t

Active Member
Licensed User
Longtime User
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:
    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

OliverA

Expert
Licensed User
Longtime User
What are you expecting? What are you getting?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Hi,
I think line no 6 should be:
B4X:
iv3(n) = conv.IntsToBytes(Val(n))
Ah, close, I think the integer to byte conversion needs to happen outside of the loop
B4X:
    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

stu14t

Active Member
Licensed User
Longtime User
@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

OliverA

Expert
Licensed User
Longtime User
64 bytes long instead 16
Of course, since each Int is 4 bytes and 16 Int(s) make 64 bytes.

Try
B4X:
    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

stu14t

Active Member
Licensed User
Longtime User
Of course, since each Int is 4 bytes and 16 Int(s) make 64 bytes.

Try
B4X:
    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

Similar Threads

Top