| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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; |
|
#2
| |||
| |||
| "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 |
|
#3
| |||
| |||
| "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 |
|
#4
| |||
| |||
| "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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.