Solved

How to write a unicode string to a ansi file ?

Posted on 2009-05-06
12
510 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now