?
Solved

How to write a unicode string to a ansi file ?

Posted on 2009-05-06
12
Medium Priority
?
518 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 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