Unfortunately, BlitzMAX doesn't encode text in streams as Unicode, which Mark says was a bit of an oversight.
Here, then, are two function that I use to write a string into a stream as unicode, and to retrieve it:
Function WriteStringW(stream:TStream,str:String)
Local shorts:Short Ptr
shorts=str.ToWString()
WriteInt stream,Len(str) ' Number of shorts to write
For Local i:Int=0 To Len(str)-1
WriteShort stream,shorts[i]
Next
MemFree shorts
End Function
Function ReadStringW:String(stream:TStream)
Local numShorts:Int
Local str:String
Local shorts:Short[]
numShorts=ReadInt(stream)
shorts=New Short[numShorts]
For Local i:Int=0 To numShorts-1
shorts[i]=ReadShort(stream)
Next
str=String.FromShorts(shorts,numShorts)
Return str
End Function
There's nothing particularly fancy about these functions, other than they work.
Internally, all BlitzMAX strings are Unicode, so the functions are pretty transparent, as far as code in concerned. I write the number of unicode characters first, then the actual characters, rather than having a null (or double-null, in this case) terminated string. Mostly a matter of convenience for me, and it eliminates the possibility of buffer overflows.
Enjoy!
Neil
|