Solved

How to write a unicode string to a ansi file ?

Posted on 2009-05-06
12
517 Views
Last Modified: 2012-05-06
I want to migration to Delphi 2009, where all strings are now unicode
Previously, it was more simple to write such string into an external file, as both where ANSI and no conversion was really needed
Now I want the real Unicode string of Delphi to be written to an external file but first, converted of course to ANSI
Who do I proceed ?
Thanks
0
Comment
Question by:LeTay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 9

Accepted Solution

by:
arioh earned 500 total points
ID: 24313149
something like: WriteToFile( AnsiString( YOU_UNICODE_STRING_HERE ) );
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 24313190
arioh's solution will work fine, but you will loose the unicode information.
0
 

Author Comment

by:LeTay
ID: 24313209
Hello arioh
Do you mean that AnsiString is a function ?
Can I have the following code :

...
var
 A:AnsiString;
 U:UnicodeString;
...
begin
...
 U := 'blabla';
...
 A := AnsiString(U);
Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:MerijnB
ID: 24313218
It means you case the unicode string to an ansi string, you can see it as if it's a function (while technically it's not).
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24313307
Hi,
is the file you are trying to save of a specific binary format, or is it just a simple text file? Because if it is a simple text-file, then you can use the TStrings.SaveToFile function to do this, and then just supplying a TEncoding property.

regards
Hypo
  aLines := TStringList.Create;
  ... // Add your Unicode text... 
  aLines.SaveToFile('C:\Test', TEncoding.GetASCII);

Open in new window

0
 
LVL 12

Expert Comment

by:Hypo
ID: 24313318
Oops, a Typo...
it should be "aLines.SaveToFile('C:\Test', TEncoding.ASCII);" instead of the above
0
 

Author Comment

by:LeTay
ID: 24313361
I have in fact now several functions that I developed in a library (DLL) to do this kind of things
My first function to converts is getting a typical string as argument (plus filename) and is pushing the entire string in the file. A third argument is a flag indicating if the string has to be converted to ANSI or not
So if yes, I will use ANSIString() function (this was the way it worked before, without the conversion of course)
If no, do I have to write the B.O.M. to the external now "unicoded" file ?
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24313459
As long as the code that is supposed to read the data later on, knows that the data is always stored in the same unicode format, then you don't have to store the byte order mark; but that is only as long as you always use the same unicode format. If you want to store the unicode in whatever format is passed on to your function (when unicode was supposed to be used), then you should probably store the bom as well.

0
 

Author Comment

by:LeTay
ID: 24313610
Well, now I have a last stupid problem
I use the blockwrite instruction to write the string to the file
Something like BlockWrite(DFile,Temp[1],SizeOf(Temp),BytesWritten)
where Temp was declared as ANSIString and contains then ANSIString(UnicodeStringInput)
It works, but SizeOf (show in debug) always returns 4 ! even if Temp now contains "blablablabla" !!!
0
 
LVL 9

Expert Comment

by:arioh
ID: 24313656
use Length( Temp )
0
 

Author Comment

by:LeTay
ID: 24313961
That's what I finally did
Use Length for ANSI and times 2 for Unicode
But I still wonder about that sizeof() that always returns 4 !
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24314100
SizeOf returns the number of bytes allocated for a variable... and the contents of a string variable is actually a pointer to some referencecounted memory containing the string data; So what SizeOf returns, is the size of the pointer that points to the string data, and a normal pointer is always 4 bytes in win32. That's why SizeOf always returns 4 for you....

regards
Hypo
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question