Android Question Define String with a certain length

MList

Member
Licensed User
Hi,

Is there any way to define a string with a certain length ? Like in Visual Basic reserved as string * 3 => length = 3
I have big data coming in as Hex String, it woud be much easier to handle with a structure of certain string length.

VISUAL BASIC
Public Type flagstringstruct 'Value Flags zb 08 hex = binary 00001000
reserved As String * 3
valid As String * 1 ' 0 valid, 1 out of range
deleted As String * 1 ' 1 entry deleted
type As String * 3 ' 00(1) = FE, 01(2) = NFE, 10(3) = FEDup, 11(4) = NFE Dup
End Type


Thanks for any help
Marion
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you have control over the format then there are much simpler and more powerful methods. B4XSerializator for example.
It also appears that there is a confusion between unicode codepoints and bytes. A single character can be encoded to 4 bytes in some cases.

I think that something like this will work for you:
B4X:
Private Sub Pad(s As String, TotalLength As Int) As String
 Do While s.Length < TotalLength
   s = s & Chr(0)
 Loop
 Return s
End Sub
 
Upvote 0

MList

Member
Licensed User
Thanks Erel,

No Pad is not what i need.
B4Xserializator seems fine. But how does he recognize the structure ?
in that exampel with Type Message (Name As String, Age As Int)
If there would be Firstname as string, LastName as string)
How does he know where firstname ends and Lastname begins ?
Must both side use the B4XSerializator ? Then i can't use it.
Or where do i tell the Serializator how long my fields are?

I just want to put a long String with Hex Data into a structure, where i exactly know how many bytes each element has.
"48616C6C6F000C"
48616C6C6F = Hallo (String, lenght = 5)
000C = 12 (integer)
want to move the hex string into structure string(10) ,zahl(4)

I just need to define a string with a specific length, but that seems not possible.

Thanks a lot
Marion
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you can use a custom type. hopefully, you would recognize this as a structure of sorts.

there are different ways of doing this. i have chosen 1 way. i have made all the fields in the structure of type string.
you could, eg, make all the fields arrays of bytes, or have some fields strings and others integers, etc.

i use your example "48616C6C6F000C", which represents the ascii string "Hallo" and the number 12.
i create a structure consisting of 2 fields: aword and anumber.
i examine the hexstring and assign its contents to the fields in my structure.
i then print out the structure's fields and their respective values.
 

Attachments

  • tst8.zip
    7.7 KB · Views: 48
  • marion.png
    marion.png
    49.7 KB · Views: 48
Upvote 0

teddybear

Well-Known Member
Licensed User
I just want to put a long String with Hex Data into a structure, where i exactly know how many bytes each element has.
"48616C6C6F000C"
48616C6C6F = Hallo (String, lenght = 5)
000C = 12 (integer)
want to move the hex string into structure string(10) ,zahl(4)

I just need to define a string with a specific length, but that seems not possible.

Thanks a lot
Marion
You exactly know how many bytes each element has, just use Substring function, you will get what you need
B4X:
    Dim HexData As String="48616C6C6F000C"
    Dim bc As ByteConverter
    Dim name As String=bc.StringFromBytes((bc.HexToBytes(HexData.SubString2(0, 10))),"utf8")
    Dim age As Short=bc.ShortsFromBytes(bc.HexToBytes(HexData.SubString(10)))(0)
    Log(name)  'Hallo
    Log(age)   '12
 
Last edited:
Upvote 0

MList

Member
Licensed User
Thank for your answers, i appreciate.
But the idea was to move the Hex string with one Command to the structure... like in Visual Basic.
So its much easier when data length changes to change structure.
but therefor i have to define a string with a certain length.

VISUAL BASIC
Public Type struct '
text As String * 3
number As String * 2
text As String * 10
float As String * 8
End Type

dim struct1 as struct

struct1 = "48616C6C6F000C............................"

name = bc.stringsfrombytes(struct1.text),"UTF8")
B4x
 
Upvote 0
Top