Yes, PString is limited to 255 bytes. I use these extension methods when I don’t know how big a string will be. Hmm, maybe you wouldn’t want to define the encoding there. BString uses 4 bytes for the size instead of PStrings 1.
[code]Sub writeBString(extends bs As BinaryStream, s As String)
bs.WriteUInt32(s.LenB)
bs.Write(s)
End Sub
Function readBString(extends bs As BinaryStream) As String
dim size As UInt32 = bs.ReadUInt32
if size > 0 then
return bs.Read(size).DefineEncoding(Encodings.UTF8)
else
return “”
end
End Function
[/code]
Do you have to use a BinaryStream / File? An sqlite database file would be a better choice if you have any flexibility in what you use. And yes, a PString is limited to 255 bytes (not chars, bytes).
Yes, a Pstring starts with a byte that gives the length of the string and so a Pstring is limited to 255 bytes. When I use a binary stream and want to output text I first write out an a value stating the give the length, in bytes, of the string. That is then followed with the string itself. Something like:
sz = LenB(pName)
bs.WriteShort sz
bs.Write pName
sz = LenB(aName)
bs.WriteShort sz
bs.Write aName
sz = LenB(aType)
bs.WriteShort sz
bs.Write aType
Here I am writing the length to a Short (2 byte) value as I know for sure that none of the strings will exceed. Later to read these three items back in the code looks like: