iOS Question Convert String representing bytes in actual bytes

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone...
i've this string

0x33303a43363a46373a30303a39353a3641

that represents some bytes.. how can i transform it in actual bytes?
i tried with for loops, byteconverter, arrays etc... nothing works like I need...
 
Solution
I haven't tried it but it should work. Only now did I understand that they were ascii codes of the string

B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
Dim BC As ByteConverter
Dim data() As Byte = BC.HexToBytes(str.SubString(2))

Dim sData As String = ""
For Each b As Byte In data
  sData=sData & chr(b)
Next
Log(sData)

aeric

Expert
Licensed User
Longtime User
It looks like a pattern of 5 digit numbers and then separate by a letter “a”.
 
Upvote 0

Star-Dust

Expert
Licensed User
Hi everyone...
i've this string

0x33303a43363a46373a30303a39353a3641

that represents some bytes.. how can i transform it in actual bytes?
i tried with for loops, byteconverter, arrays etc... nothing works like I need...
you have to remove "0x"

B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
Dim BC As ByteConverter
Dim data() As Byte = BC.HexToBytes(str.SubString(2))
Log((str.Length-2)/2)
Log(str)
Log(data.Length)
Dim sData As String = ""
For Each b As Byte In data
        sData=sData & b & " "
 Next
Log(sData)

LOG
17
0x33303a43363a46373a30303a39353a3641
17
51 48 58 67 54 58 70 55 58 48 48 58 57 53 58 54 65
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
you have to remove "0x"

B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
Dim BC As ByteConverter
Dim data() As Byte = BC.HexToBytes(str.SubString(2))
Log((str.Length-2)/2)
Log(str)
Log(data.Length)
Dim sData As String = ""
For Each b As Byte In data
        sData=sData & b & " "
 Next
Log(sData)
Thanks for the answer but this is not what I meant..
i mean that the result should be:

B4X:
LOG
17
0x33303a43363a46373a30303a39353a3641
17
33 30 3a 43 36 3a 46 37 3a 30 30 3a 39 35 3a 36 41

otherwise the result is:
byte_false.jpg


instead of:
byte_true.PNG
 
Upvote 0

Star-Dust

Expert
Licensed User
Thanks for the answer but this is not what I meant..
i mean that the result should be:

B4X:
LOG
17
0x33303a43363a46373a30303a39353a3641
17
33 30 3a 43 36 3a 46 37 3a 30 30 3a 39 35 3a 36 41

otherwise the result is:
View attachment 130311

instead of:
View attachment 130312
Do you want it in hex?
Just convert each byte back to hex

B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
Dim BC As ByteConverter
Dim data() As Byte = BC.HexToBytes(str.SubString(2))
Log((str.Length-2)/2)
Log(str)
Log(data.Length)
Dim sData As String = ""
For Each b As Byte In data
        sData=sData & BC.HexFromBytes(Array As Byte (b)) & " "
Next
Log(sData)

17
0x33303a43363a46373a30303a39353a3641
17
33 30 3A 43 36 3A 46 37 3A 30 30 3A 39 35 3A 36 41

But I would more simply break the string if you want it hex in string format
B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
str=str.SubString(2)
Dim HexString(str.Length/2) As String
Dim sData As String = ""
   
For i=0 To str.Length-1 Step 2
        HexString(i/2)=str.SubString2(i,i+2)
        sData=sData & HexString(i/2) & " "
Next
Log(sData)

But what exactly do you want to achieve?
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
But what exactly do you want to achieve?
What I need is to convert the String "0x33303a43363a46373a30303a39353a3641" into an Byte Array containg the exact same numbers etc, not their representation in byte.. i don't know if i explained it very well...

the end result is to convert this string "0x33303a43363a46373a30303a39353a3641" to this "30:C6:F7:00:95:6A" using then ByteToString
 
Upvote 0

Star-Dust

Expert
Licensed User
What I need is to convert the String "0x33303a43363a46373a30303a39353a3641" into an Byte Array containg the exact same numbers etc, not their representation in byte.. i don't know if i explained it very well...

the end result is to convert this string "0x33303a43363a46373a30303a39353a3641" to this "30:C6:F7:00:95:6A" using then ByteToString
Unfortunately, it is not always easy to understand the matter. I'm sorry, I answered for what I understand.

To me a Byte Array is a set of Bytes, and that's what you get in the first example.
Maybe you want a specific type of representation of this ByteArray.

More than this I don't know how to help you because I'm not sure I understand correctly

to this "30:C6:F7:00:95:6A" using then ByteToString
PS. Maybe you meant StringFromBytes
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Unfortunately, it is not always easy to understand the matter. I'm sorry, I answered for what I understand.

To me a Byte Array is a set of Bytes, and that's what you get in the first example.
Maybe you want a specific type of representation of this ByteArray.

More than this I don't know how to help you because I'm not sure I understand correctly
Don't worry. Thanks to you in any case for your time.

I try to explain better.

Like the number 1 can be represented in various way:
"1" (String)
1 (Int)
0x01 (Hex)

Also what I (unfortunately) have is a Byte Array but for ugly reasons is represented in String.

So i need
"0x33303a43363a46373a30303a39353a3641" (String) ------ become ----> Array as Byte (33, 30, 3a, 43, 36, 3a, 46, 37, 3a, 30, 30, 3a, 39, 35, 3a, 36, 41)

In this manner i can take them and put inside ByteToString(bytes, 0, length, "UTF8") and finally get ---> "30:C6:F7:00:95:6A" (String)
 
Upvote 0

Star-Dust

Expert
Licensed User
Don't worry. Thanks to you in any case for your time.

I try to explain better.

Like the number 1 can be represented in various way:
"1" (String)
1 (Int)
0x01 (Hex)

Also what I (unfortunately) have is a Byte Array but for ugly reasons is represented in String.

So i need
"0x33303a43363a46373a30303a39353a3641" (String) ------ become ----> Array as Byte (33, 30, 3a, 43, 36, 3a, 46, 37, 3a, 30, 30, 3a, 39, 35, 3a, 36, 41)

In this manner i can take them and put inside ByteToString(bytes, 0, length, "UTF8") and finally get ---> "30:C6:F7:00:95:6A" (String)
Can't you just insert colon at regular intervals every 2 characters?

So like
B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
str=str.SubString(2)

Dim sData As String = ""
For i=0 To str.Length-1 Step 2
    sData=sData & str.SubString2(i,i+2) & ":"
Next
Log(sData)
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Can't you just insert colon at regular intervals every 2 characters?

So like
B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
str=str.SubString(2)

Dim sData As String = ""
For i=0 To str.Length-1 Step 2
    sData=sData & str.SubString2(i,i+2) & ":"
Next
Log(sData)
But.. in this way the result wouldn't be --> "33:30:3a:43:36:3a:46:37:3a:30:30:3a:39:35:3a:36:41"?
 
Upvote 0

Star-Dust

Expert
Licensed User
But.. in this way the result wouldn't be --> "33:30:3a:43:36:3a:46:37:3a:30:30:3a:39:35:3a:36:41"?
Only now I understand what you want to achieve. :rolleyes:

I have to think about it to give you an answer
 
Upvote 0

Star-Dust

Expert
Licensed User
I haven't tried it but it should work. Only now did I understand that they were ascii codes of the string

B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
Dim BC As ByteConverter
Dim data() As Byte = BC.HexToBytes(str.SubString(2))

Dim sData As String = ""
For Each b As Byte In data
  sData=sData & chr(b)
Next
Log(sData)
 
Upvote 2
Solution

Mike1970

Well-Known Member
Licensed User
Longtime User
I haven't tried it but it should work. Only now did I understand that they were ascii codes of the string

B4X:
Dim str As String = "0x33303a43363a46373a30303a39353a3641"
Dim BC As ByteConverter
Dim data() As Byte = BC.HexToBytes(str.SubString(2))

Dim sData As String = ""
For Each b As Byte In data
  sData=sData & chr(b)
Next
Log(sData)
Ouh... i did't know about Chr
 
Upvote 0
Top