Android Question Convert string to byte array

nicomx

Member
Licensed User
Longtime User
Hi everyone.

I've been reading the forum for two days and I'm getting more and more lost. I'll tell you the problem (it's probably already answered and I'm simply useless)

I have a text string with this format.

B4X:
7C54C10DFCE9

And I need, based on it, to output a byte array with the format below.

B4X:
0x7C,0x54,0xC1,0x0D,0xFC,0xE9

I have tried several times and either it throws me errors, or it shows me integers...

If I create the arraay by hand it works great

B4X:
Dim keyb() As Byte = Array As Byte(0x7C,0x54,0xC1,0x0D,0xFC,0xE9)


But I want the array to be created based on the string, dynamically. I can't find a way.
 

Quandalle

Member
Licensed User
B4X:
    Dim s As String ="7C54C10DFCE"
    Dim res As String
    For i=0 To s.Length-2 Step 2
        res = res & IIf(i=0,"0x",",0x") & s.SubString2(i,i+2)
    Next
    If s.Length Mod 2 = 1 Then res = res & ",Ox" & s.SubString(s.length-1) 'if the length is odd add the last
    Log (res)
 
Upvote 1

nicomx

Member
Licensed User
Longtime User
B4X:
    Dim s As String ="7C54C10DFCE"
    Dim res As String
    For i=0 To s.Length-2 Step 2
        res = res & IIf(i=0,"0x",",0x") & s.SubString2(i,i+2)
    Next
    If s.Length Mod 2 = 1 Then res = res & ",Ox" & s.SubString(s.length-1) 'if the length is odd add the last
    Log (res)
Thanks a lot!!! I've been searching for this for days!

I think now I will be able to dynamically compose the byte strings based on your solution.

Thank you very much again :)

B4X:
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
FA4505E8A13B
0xFA,0x45,0x05,0xE8,0xA1,0x3B
7C54C10DFCE9
0x7C,0x54,0xC1,0x0D,0xFC,0xE9
97F4ABB8A69E
0x97,0xF4,0xAB,0xB8,0xA6,0x9E
41AF3647F5AA
0x41,0xAF,0x36,0x47,0xF5,0xAA
CB6E5FAB62D1
0xCB,0x6E,0x5F,0xAB,0x62,0xD1
FBE1B1AF3742
0xFB,0xE1,0xB1,0xAF,0x37,0x42
CE29735A0F58
0xCE,0x29,0x73,0x5A,0x0F,0x58
BA0DA761E6CC
0xBA,0x0D,0xA7,0x61,0xE6,0xCC
8C68103830CF
0x8C,0x68,0x10,0x38,0x30,0xCF
9100DF763A73
0x91,0x00,0xDF,0x76,0x3A,0x73

Awesome!!
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
B4X:
    Dim s As String ="7C54C10DFCE"
    Dim res As String
    For i=0 To s.Length-2 Step 2
        res = res & IIf(i=0,"0x",",0x") & s.SubString2(i,i+2)
    Next
    If s.Length Mod 2 = 1 Then res = res & ",Ox" & s.SubString(s.length-1) 'if the length is odd add the last
    Log (res)

If length is odd - the single letter should be the firstly decoded.

B4X:
Dim s As String ="7C54C10DFCE"
    Dim res As String, j as Int
    If s.Length Mod 2 = 1 Then
        res = "0x" & s.SubString(0, 1) & ", " 'if the length is odd add the first
        j = 1
    Else
        j = 0
    end if
    For i=j To s.Length-2 Step 2
        res = res & IIf(i=0,"0x",",0x") & s.SubString2(i,i+2)
    Next
    Log (res)
 
Upvote 0

Quandalle

Member
Licensed User
nicomx didn't specify which format he wanted in case the string length is odd.
for example for : 7C54C12 it could be :
  • 0x7C,0x54,0xC1,0x2 (see post #2)
  • 0x7,0xC5,0x4C,0x12 (see post #4)
  • 0x07,0xC5,0x4C,0x12 which keeps each item the same length
In this last case the code would be :
B4X:
   Dim s As String ="7C54C10DFCE"
   Dim res As String
   if s.length mod 2 = 1 then s = "0" & s
    For i=0 To s.Length-2 Step 2
        res = res & IIf(i=0,"0x",",0x") & s.SubString2(i,i+2)
    Next
    Log (res) ' expected 0x07,0xC5,0x4C,0X10,0xDF,0xCE
 
Upvote 0
Top