Converting an AnsiString to a Unicode String
Posted
by jrodenhi
on Stack Overflow
See other posts from Stack Overflow
or by jrodenhi
Published on 2010-04-01T00:12:32Z
Indexed on
2010/04/01
2:53 UTC
Read the original article
Hit count: 283
delphi
I'm converting a D2006 program to D2010. I have a value stored in a single byte per character string in my database and I need to load it into a control that has a LoadFromStream, so my plan was to write the string to a stream and use that with LoadFromStream. But it did not work. In studying the problem, I see an issue that tells me that I don't really understand how conversion from AnsiString to Unicode string works. Here is some code I am puzzling over:
oStringStream := TStringStream.Create(sBuffer);
sUnicodeStream := oPayGrid.sStream; //explicit conversion to unicode string
iSize1 := StringElementSize(oPaygrid.sStream);
iSize2 := StringElementSize(sUnicodeStream);
oStringStream.WriteString(sUnicodeStream);
When I get to the last line, iSize1 does equal 1 and iSize2 does equal 2, so that part is what I understood from my reading. But, on the last line, after I write the string to the stream, and look at the Bytes Property of the string, it shows this (the string starts as '16,159'):
(49 {$31}, 54 {$36}, 44 {$2C}, 49 {$31}, 53 {$35}, 57 {$39} ...
I was expecting that it might look something like
(49 {$31}, 00 {$00}, 54 {$36}, 00 {$00}, 44 {$2C}, 00 {$00},
49 {$31}, 00 {$00}, 53 {$35}, 00 {$00}, 57 {$39}, 00 {$00} ...
I'm not getting the right results out of the LoadFromStream because it is reading from the stream two bytes at a time, but the data it is receiving is not arranged that way. What is it that I should do to give the LoadFromStream a well formed stream of data based on a unicode string?
Thank you for your help.
© Stack Overflow or respective owner