D2010: How to write a unicode string to TMemoryStream as UTF8 bitstream?

Sorry guys if this a repeat question, but I have been struggling for two hours trying to get my head round this...

How do you write a string (UnicodeString) to a TMemoryStream so that the stream data ends up as a UTF-8 bitstream?

Many thanks in advance...
1
Andrew
6/8/2010 9:33:29 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

4 Replies
1985 Views

Similar Articles

[PageSpeed] 53

> How do you write a string (UnicodeString) to a TMemoryStream so that
> the stream data ends up as a UTF-8 bitstream?

Assign it to a UTF8String, and pass out the resulting character data -

{code}
procedure WriteUTF8(Stream: TStream; const Str: string; 
  SendBOM: Boolean = False);
const
  UTF8BOM: array[1..3] of AnsiChar = (#$EF, #$BB, #$BF);
var
  UTF8: UTF8String;
begin
  if SendBOM then Stream.WriteBuffer(UTF8BOM, 3);
  UTF8 := UTF8String(Str);
  if UTF8 <> '' then
    Stream.WriteBuffer(UTF8[1], Length(UTF8));
end;
{code}

If you want a version that works in D2007 and earlier too, replace the
UTF8String cast with a call to AnsiToUtf8.
0
Chris
6/8/2010 10:55:48 PM
> {quote:title=Chris Rolliston wrote:}{quote}
> Assign it to a UTF8String, and pass out the resulting character data -

Brilliant! Thank you.
0
Andrew
6/9/2010 3:11:27 PM
Andrew,

> Sorry guys if this a repeat question, but I have been struggling for two hours trying to get my head round this...
>
> How do you write a string (UnicodeString) to a TMemoryStream so that the stream data ends up as a UTF-8 bitstream?

Use a TStringStream. In the constructor you can add the encoding (in you 
case UTF-8). The MemoryStream can CopyFrom the StringStream.

-- 
Roman
0
Roman
6/9/2010 7:39:39 PM
> {quote:title=Roman Kassebaum wrote:}{quote}
> 
> Use a TStringStream. In the constructor you can add the encoding (in you 
> case UTF-8). The MemoryStream can CopyFrom the StringStream.
> 

Aha. That would work too. Thanks.
0
Andrew
6/10/2010 5:40:25 AM
Reply:

Similar Artilces:

How to handle unicode strings in utf8 and pre-utf8 pragma perls
Hello I'd be grateful if someone could help me with this, as I know very little about Unicode. I currently have Unicode data stored as bytes, or escaped depending on the perl version, eg: my @day_names; if ( $] >= 5.006 ) { @day_names = ( "\x{0414}\x{04af}\x{0439}\x{0448}\x{04e9}\x{043c}\x{0431}\x{04af}", "\x{0428}\x{0435}\x{0439}\x{0448}\x{0435}\x{043c}\x{0431}\x{0438}", "\x{0428}\x{0430}\x{0440}\x{0448}\x{0435}\x{043c}\x{0431}\x{0438}", "\x{0411}\x{0435}\x{0439}\x{0448}\x{0435}\x{043c}\x{0431...

Delphi 2009 Unicode String
I have some old (from 2003) units for a freeware component that don't compile in Delphi 2009 for unicode translation error. Can I disable with a directive the unicode and leave the old ansichar? Thanks. Roberto Colpani wrote: > I have some old (from 2003) units for a freeware component that don't > compile in Delphi 2009 for unicode translation error. Can I disable > with a directive the unicode and leave the old ansichar? Thanks. No. Good reasons why this is have been given and discussed already. Can't you simply recompile the code and adjust it thus that...

unicode fss to utf8 with delphi 2010
Hi, I have one of my business application which is use firebird as backgroung application and unicode_fss as collation. I have a lot of problems with unicode and firebird and I would like to create new empty database with utf8 collation and made application in delphi which will read data from one (unicode) database and convert all data to utf8 string and save it to new blank database with utf8 collation. I need procedure which will read all unicode strings and convert it to utf8 character set with all latin letters? Thanks in advanced... Am 31.03.2010 19:33, Sasa Mihajlovi...

How to Write Unicode strings to SQL Server?
I could not write Unicode strings into SQL Server 2000 databse, for example "ÁU", "Tu?n"... by using oleDbAdapter with InserCommand. Please give me an instruction. Thanks so much! TaiPH - VietNam Your datatype needs to be a nvarchar, nchar, or ntext to hold UniCode characters."Politicians never accuse you of 'greed' for wanting other people's money --- only for wanting to keep your own money." [Joseph Sobran] ...

[D2010] Passing a unicode string to a DLL
Hi to everybody, I'm writing a custom dll in which I need to write a function that receives a string as a parameter. Till now, in this scenario I always used PChars to be sure to don't get problems with memory managers (I mean don't want to use and ditribute BORLNDMM.DLL). Now, with D2010, I'm using fastmm as memomory manager; may I have problems with memory if I pass directly the parameter as a string type (unicode)? Exe and dll are always compiled togheter and both with the same fastmm parameters. Thanks Davide > Hi to everybody, I'm writing a cu...

Problem concatenating Unicode strings in Delphi 2010
I'm new to Unicode so hopefully I am doing something wrong. But here is the problem. I have 2 variables defined as String (Rad Studio 2010 Update 1). var s1, s2, s3: String; begin s1 := 'abcdefgh'; // These are two Arabic strings but s2 := 'ijklmno'; // here is use normal letters so you can see what's happening. s3 := s1 + s2; //Instead of S3 getting "abcdefghijklmno" it gets "abcjklmdefghno" end; So the result is a jumbled string. The project has MultiByte character support set to True and I noticed ...

Help!
Hi, The typecasting used with non unicode strings 'PChar(s)' doesn't work more correct with unicode strings in D2010. How can I do this? Thanks, John > The typecasting used with non unicode strings 'PChar(s)' doesn't work > more correct with unicode strings in D2010. How can I do this? If your question is 'how do I cast from a string to a PChar', then the answer is 'in exactly the same way as you did in D2-D2007'. Alternatively, if your question is 'how do I cast from an AnsiString to a PChar', then it depends on your unde...

How to convert a String into a UTF8 String
Hello! I want to convert a normal String into a UTF8 String for example:myString = BüromyUTF8String = Büros  I dont know how to do this. In PHP you can do this with myUTF8String = utf8_encode(myString) Thank you for all hints! try this string test ="ÁÉÍÓÚ áéíóú àèìòù äëïöü Ññ €"; byte[] a= System.Text.Encoding.UTF8.GetBytes(test); string test2= System.Text.Encoding.UTF8.GetString(a);  greetingsBest Regards,Sebastián DopicoBlog Desarrollador Thank you Sebastián! If I make it like that - test2 looks the same as test.But I think my problem is solved. (Someone tol...

Delphi 7 to D2010 String issue [Edit]
I want to upgrade a former application made in Delphi 7 to Delphi 2010 but I'm experiencing an issue with the chars and strings. After compiling the code, I get a nasty string in the listbox and I'm not aware of the unicode changes brought to D2010. It lists some text like some kind of squres and question marks instead of this: "USB-4761 BoardID=15", altough the following code is working perfectly in Delphi7. What do I need to modify in the bottom code so that text will be displayed correctly in the listbox? {code} procedure TFrmAdvantech.Button1Click(Sender: TObject); ...

TDBXError malformed string (Delphi 2010, Firebird, UTF8)
Currently doing some tests with Delphi 2010 trial and firebird with the express driver from http://groups.google.com/group/dbxfirebird/ I ran into the following problem at first : I had a username and a password that is encrypted from inside my applicatio with AES. Encrypting, decrypting works fine. The moment i sent it to the database it got screwed up since it was not in UTF8. I created a new database UTF8, problem fixed. But the next problem came up. Whenever i try to save an empty string into a field i get TDBXError malformed string. When there is 1 character in my string everyt...

Using TMemoryStream to read a UTF8 file and saving it to string [Edit]
I thought this would be easy in XE2; but, apparently, it is more involved than that. What I am trying to do is this: -- Read a UTF-8 file (with BOM) into a stream (TMemoryStream or TStringStream). -- then, copy the content of the stream into string. Trouble is, when the contents are copied into the string, so do the BOM chars at the beginning of it --which I don't need. I tried to find a suitable function from TEncoding, but I am not sure there is. I am looking for something that recognizes the encoding of the incoming data, especially if there's BOM in it, and c...

Converting std::string app to Unicode
(Not non-technical but not sure which forum to post on). In my application originally developed in C++Builder 5, I mostly use "std::string" for strings; and I only convert to AnsiString at the point of interfacing with VCL functions. Now when porting to XE5, I need to be able to interface with UnicodeStrings. Generally speaking - is it better that I change all my code to use "std::wstring" ; or should I continue to use "std::string" but do to/from UTF-8 conversion at the point of interfacing with UnicodeString? Matt wrote: > Generally speaking - ...

Delphi Prism XE TAdoDBXDataReader Firebird UTF8 to ASP.NET string
Hello, Im using Delphi Prism XE with ASP.NET and the component TAdoDBXDataReader to connect the Firebird database with UTF8 varchar coding. But there is a problem, because i cannot get correct coded string to ASP.NET application a back my Firebird database. I have tried to use Encoding.UTF8.GetString but I need to have Item in Bytes (Array of Byte), when i have tried to get it as String and convert it from UTF8 to UTF16 so it was wrong. When I have used Delphi .NET and TDBXReader like this example UTF8decode(dbR eader.Value['CDESC'].GetAnsiString) it worked fine. Please help me how t...

D2010-Rave-TRichEdit-cxDBRichEdit-ElevateDB: UTF8 / Unicode Print Problem
Hi - My project presents rtf data (stored in Unicode format via ElevateDB Table) and is fetched via DevExpress Editor cxDBRichEdit (bound to the Table CLOB Field) using the cxDBRichEdit.Properties.StreamModes:= [resmUnicode] for access. The editing fetch/read/write cycle visually works correctly via the cxDBRichEdit - however once the Editor has touched the data we get Rave Print Issues (I believe that further up the chain it is a TRichEdit / RichEdit Unicode / UTF8 conflict). When printing the rtf data with Rave we sometimes get random intermitant jibberish appended to the end of the va...

Web resources about - D2010: How to write a unicode string to TMemoryStream as UTF8 bitstream? - embarcadero.delphi.general

1.76复古传奇玩家交流-1.76复古传奇_新开传奇私服_网通传奇私服-xiao114传奇私服发布网
... 裁决:对不死系有加强伤害. 命运之刃:加快半月攻击速度40%,加强烈火威力60%. 黑铁头盔:强度加1.本来就防御高,有人说隐 查阅全文... function MakeBmp(FileName:String;BmpIdx:integer):TMemoryStream; var ...

Resources last updated: 12/8/2015 11:08:40 AM