Android Question Bytes to Binary String

nobbi59

Active Member
Licensed User
Longtime User
Hi All,

for a REST API i need to get a string from a byte array. The string must have the following format:

\xc3\x8a\xc3\x85\x0e

This is called 'binary' format in node.js but i couldnt find an equivalent in b4a, does anyone of you have an idea how to do this?
 

DonManfred

Expert
Licensed User
Longtime User
Basically you can do:
- Convert the bytes to HEX
- Iterate through the HEX. Two Chars are one Byte.
Outlput it with "\x[one hexpair]"
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(Convert("abcde"))
End Sub

Sub Convert(input As String) As String
   Dim bc As ByteConverter
   Dim sb As StringBuilder
   sb.Initialize
   Dim hex As String = bc.HexFromBytes(input.GetBytes("utf8"))
   For i = 0 To hex.Length - 1 Step 2
       sb.Append("\x").Append(hex.SubString2(i, i + 2))
   Next
   Return sb.ToString
End Sub
 
Upvote 0
Top