Solved

How to write a unicode string to a ansi file ?

Posted on 2009-05-06
12
511 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 96
Print Graphic and Text to Epson TM-T88v 12 184
Dynamically Created Query 3 50
Dev express lookupcombo 3 13
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

18 Experts available now in Live!

Get 1:1 Help Now