Solved

How to write a unicode string to a ansi file ?

Posted on 2009-05-06
12
512 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SUM 2 INTEGER ARRAYS INTO 1 10 100
how to update exe applicatio from internet ? 6 78
Delphi and Access based Enumeration 9 64
PHP preg_replace code convert to Delphi 14 57
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…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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