BinaryStream: I miss a CString Method

I was fooling around yesterday evening and fall into a trap… I had a CString to read (using a BinaryStream) and do not found an instruction to use.

Yes, MemoryBlock have that, but then I have to load the file, set it to a MemoryBlock…

But CString exists ! I can Dim one…

What must I do ?

Use a MemoryBlock(512)…
Use a loop and search for the ending Chr(0) Byte ?

Advices are welcome.

BinaryStream has never had a ReadCString or WriteCString function. Unless the file is 10s of megabytes, just read it into a memoryblock.

But there is no reason you would ever need to. CString exists for use in Declares and Xojo automatically converts String to CString and back as necessary.

I am reading a rar file (but it can be a zip file) using a BinaryStream. And my Hex Editor tells me that I have a file name that is stored as CString.

Not related to Declare at all.

No, you read the stream straight into a MemoryBlock, not a String.

No need to guess the structure. And it is not a CString.

Perhaps what you’re missing is the ReadPString method.

Reading a RAR header is not simple because many fields don’t have fixed positions.

For example, a file name uses 2 fields, length+name, but differently of Pascal, both have a variable size.

Length use VInt, a special kind of unsigned integer of variable size, if the filename in bytes has less then 128 bytes, VInt is a UInt8, if it is a bit larger, UInt16, and keeps growing until UInt64.

I do not had internet then.

A string that ends with a Null ? ($00)…

(but it started with a "")

No, and I saw it.

The string is:
“\0000.jpg” and a $00.
In hex: 5C 30 30 30 31 2E 6A 70 67 00

Thank you for the link.

No. A CString yes, but not all string is a CString.

If you read the structure I’ve presented, it must be treated as a coincidence having a null there.

UnRAR Source for deeper studies:

If you’re trying to access the content of a RAR archive, you can use my wrapper for the UnRAR DLL