ansi / unicode

This is a discussion on ansi / unicode within the Delphi forums in Programming Languages category; What does it take to transform an ANSI text file into a unicode one? My example below (D7) inputs and outputs the exact same stuff, 31 characters, some non-ASCII. procedure TForm1.Button1Click(Sender: TObject); var f: textfile; s: ansistring; u: widestring; begin assignfile(f,'ansi.txt'); reset(f); readln(f,s); closefile(f); u:= s; assignfile(f,'unicode.txt'); rewrite(f); writeln(f,u); closefile(f); end;...

Go Back   Application Development Forum > Programming Languages > Delphi

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-05-2008, 04:42 PM
Uffe Kousgaard
Guest
 
Default ansi / unicode

What does it take to transform an ANSI text file into a unicode one?

My example below (D7) inputs and outputs the exact same stuff, 31
characters, some non-ASCII.

procedure TForm1.Button1Click(Sender: TObject);
var
f: textfile;
s: ansistring;
u: widestring;
begin
assignfile(f,'ansi.txt');
reset(f);
readln(f,s);
closefile(f);

u:= s;

assignfile(f,'unicode.txt');
rewrite(f);
writeln(f,u);
closefile(f);
end;



Reply With Quote
  #2  
Old 08-05-2008, 05:15 PM
Remy Lebeau \(TeamB\)
Guest
 
Default Re: ansi / unicode


"Uffe Kousgaard" <oh@no.no> wrote in message
news:4898bb42@newsgroups.borland.com...

> My example below (D7) inputs and outputs the exact same stuff,
> 31 characters, some non-ASCII.


The TextFile type does not support Unicode. You will have to change your
code to use the VCL's or Win32 API's file I/O instead of Pascal file I/O, at
least for the output file, for example:

procedure TForm1.Button1Click(Sender: TObject);
var
f: TextFile;
o: Integer;
s: AnsiString;
u: WideString;
begin
AssignFile(f, 'ansi.txt');
Reset(f);
ReadLn(f, s);
CloseFile(f);

u := s;

o := FileCreate('unicode.txt');
if u <> '' then
FileWrite(o, u[1], Length(u) * SizeOf(WideChar));
FileClose(o);
end;


Gambit


Reply With Quote
  #3  
Old 08-06-2008, 03:53 AM
Uffe Kousgaard
Guest
 
Default Re: ansi / unicode

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:4898c361@newsgroups.borland.com...
>
> The TextFile type does not support Unicode. You will have to change your
> code to use the VCL's or Win32 API's file I/O instead of Pascal file I/O,
> at least for the output file, for example:


Thanks, I added a bit more code, like this:

type
TUnicodeFileStream = class(TFileStream)
public
constructor Create(const FileName: string);
procedure WriteString(u: widestring);
procedure WriteLn;
end;

constructor TUnicodeFileStream.Create(const FileName: string);
const
CUnicodeNormal: WideChar = WideChar($FEFF);
begin
inherited Create(filename,fmCreate);
Write(CUnicodeNormal,sizeof(WideChar));
end;

procedure TUnicodeFileStream.WriteString(u: widestring);
begin
if u<>'' then Write(u[1],Length(u)*SizeOf(WideChar));
end;

procedure TUnicodeFileStream.WriteLn;
const
CUnicodeCR: WideChar = WideChar($000D);
CUnicodeLF: WideChar = WideChar($000A);
begin
Write(CUnicodeCR,sizeof(WideChar));
Write(CUnicodeLF,sizeof(WideChar));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
f: textfile;
s: ansistring;
u: widestring;
ff: TUnicodeFileStream;
begin
assignfile(f,'ansi.txt');
reset(f);
readln(f,s);
closefile(f);

u:= s;

ff:= TUnicodeFileStream.Create('unicode2.txt');
ff.WriteString(u);
ff.WriteLn;
ff.Free;
end;

See also http://17slon.com/gp/gp/files/gptextfile.htm


Reply With Quote
  #4  
Old 08-06-2008, 01:31 PM
Remy Lebeau \(TeamB\)
Guest
 
Default Re: ansi / unicode


"Uffe Kousgaard" <oh@no.no> wrote in message
news:48995878$1@newsgroups.borland.com...

> CUnicodeNormal: WideChar = WideChar($FEFF);


If you are going to write a BOM to the Unicode file, you could alternative
use UTF-8 instead, since Ansi is compatible with it. That way, you could
preserve your Ansi data as-is, and the BOM will alert any Unicode-aware app
opening the file to convert the UTF-8 to Unicode.


Gambit


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:44 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.