Board index » delphi » Convert Unicode strings to ANSI strings?

Convert Unicode strings to ANSI strings?

I've Googled and searched the FAQs in vain. I have a large number of files
which the originating application saved as Unicode. I need to batch-convert
them to a human-readable/searchable form and for further Delphi processing.
Basically, I need to get rid of Unicode, i.e. convert strings to ANSI (the
text is all English ansi, there was no need for Unicode in the first
place).

I've tried readln, but I get garbage. I've tried WideCharToString, but I
get garbage. I've tried Mike Lischke's unicode.pas, but it has no tools to
read strings from files or to convert from unicode to ansistrings. All I
need to do is

        readln( inFile, aUnicodeStr );
        aAnsiStr := ???SomeConversionProc( aUnicodeStr );
        writeln( outFile, aAnsiStr );

Thanks a lot in advance,

.marek jedlinski

--
No ads, no nags freeware: http://keynote.prv.pl
(KeyNote, PhoneDeck, KookieJar, Oubliette)

 

Re:Convert Unicode strings to ANSI strings?


Have you tried reading the files into a WideString using a TFileStream

AAnsiiString := AWideString ;

Should handle the conversion for you 'seamlessly'

On Tue, 05 Nov 2002 23:17:53 +0100, marek jedlinski

Quote
<eris...@lodz.pdi.net> wrote:
>I've Googled and searched the FAQs in vain. I have a large number of files
>which the originating application saved as Unicode. I need to batch-convert
>them to a human-readable/searchable form and for further Delphi processing.
>Basically, I need to get rid of Unicode, i.e. convert strings to ANSI (the
>text is all English ansi, there was no need for Unicode in the first
>place).

>I've tried readln, but I get garbage. I've tried WideCharToString, but I
>get garbage. I've tried Mike Lischke's unicode.pas, but it has no tools to
>read strings from files or to convert from unicode to ansistrings. All I
>need to do is

>    readln( inFile, aUnicodeStr );
>    aAnsiStr := ???SomeConversionProc( aUnicodeStr );
>    writeln( outFile, aAnsiStr );

>Thanks a lot in advance,

>.marek jedlinski

>--
>No ads, no nags freeware: http://keynote.prv.pl
>(KeyNote, PhoneDeck, KookieJar, Oubliette)

Re:Convert Unicode strings to ANSI strings?


Here you go
- watch out for the Unicode trash being 2 bytes per 'character'
- it might be an idea to check that all files have a length that is
even

procedure TForm1.Button1Click(Sender: TObject);
Const Fle = 'c:\t\test.txt';
Var
  FS :TFileStream;
  S :String;
  WS :WideString;
  L :Integer;
begin
  // Create a Unicode File
  WS := 'This is Unicode Trash';
  FS := TFileStream.Create( Fle, fmCreate );
  FS.Write( WS[1], Length(WS)*2 );
  FS.Free;
  // Read it back
  FS := TFileStream.Create( Fle, fmOpenRead );
  L := FS.Size;
  SetLength( WS, L Div 2 );
  FS.Read( WS[1], L );
  FS.Free;
  //
  S := WS;
  ShowMessage( S );
end;

Re:Convert Unicode strings to ANSI strings?


Im Artikel <brggsukor8g4sb8u0a1qgko258kmmu7...@4ax.com>, marek jedlinski
<eris...@lodz.pdi.net> schreibt:

Quote
>I've tried readln, but I get garbage.

What Delphi version do you use? Reading into a WideString variable should work,
and also the conversion should be done automatically when assigning the result
to an AnsiString variable.

Quote
>text is all English ansi

Then you also can read the files by characters, and drop every second (#0)
char.
While not eof(src) do begin Read(src, c1, c2); Write(dst, c1); end;

Everything untested...

DoDi

Re:Convert Unicode strings to ANSI strings?


On Wed, 06 Nov 2002 01:18:42 GMT, Bounce_It_je...@iss.u-net.com_.bin (J

Quote
French)  wrote:
>Have you tried reading the files into a WideString using a TFileStream

>AAnsiiString := AWideString ;

>Should handle the conversion for you 'seamlessly'

I expected that, but it doesn't.

readln( inf, aWideString );
aAnsiString := aWideString;
writeln( outf, aAnsiString );

- this puts unicode back into the output file, and adds another puzzle: the
output file is now longer by 2 bytes than the input file. (The two string
variables are declared and WideString and AnsiString, respectively).

.marek

Re:Convert Unicode strings to ANSI strings?


It certainly does add another puzzle !

WriteLn writes the data and then writes #13#10
(end of line)

Here is an extended version
- and in D4 it certainly converts Unicode to ANSII

procedure TForm1.Button1Click(Sender: TObject);
Const
  U_Fle = 'c:\t\testu.txt';
  A_Fle = 'c:\t\testa.txt';
Var
  FS :TFileStream;
  S :String;
  WS :WideString;
  L :Integer;
begin
  // Create a Unicode File
  WS := 'This is Unicode Trash';
  FS := TFileStream.Create( U_Fle, fmCreate );
  FS.Write( WS[1], Length(WS)*2 );
  FS.Free;
  // Read it back
  FS := TFileStream.Create( U_Fle, fmOpenRead );
  L := FS.Size;
  SetLength( WS, L Div 2 );
  FS.Read( WS[1], L );
  FS.Free;
  //
  S := WS;
  ShowMessage( S );
  // Write ANSII
  FS := TFileStream.Create( A_Fle, fmCreate );
  FS.Write( S[1], Length(S) );
  FS.Free;

end;

On Fri, 08 Nov 2002 00:02:43 +0100, marek jedlinski

Quote
<eris...@lodz.pdi.net> wrote:
>On Wed, 06 Nov 2002 01:18:42 GMT, Bounce_It_je...@iss.u-net.com_.bin (J
>French)  wrote:

>>Have you tried reading the files into a WideString using a TFileStream

>>AAnsiiString := AWideString ;

>>Should handle the conversion for you 'seamlessly'

>I expected that, but it doesn't.

>readln( inf, aWideString );
>aAnsiString := aWideString;
>writeln( outf, aAnsiString );

>- this puts unicode back into the output file, and adds another puzzle: the
>output file is now longer by 2 bytes than the input file. (The two string
>variables are declared and WideString and AnsiString, respectively).

>.marek

Re:Convert Unicode strings to ANSI strings?


Quote
"marek jedlinski" <eris...@lodz.pdi.net> wrote in message

news:brggsukor8g4sb8u0a1qgko258kmmu749i@4ax.com...
<snip>
Quote
> Basically, I need to get rid of Unicode, i.e. convert strings to
ANSI (the
> text is all English ansi, there was no need for Unicode in the first
> place).

<snip>

I had the same problem earlier this year and after much umming and
ahhing I came up with the following solution, this converts a
Microsoft Winmsd report into an ASCII version of it, there's probably
a better way to define the various arrays etc but it works for the
application I've put it too :

procedure TForm1.FormShow(Sender: TObject);
var
  Unifile : file;
  txtfile : textfile;
  numread : integer;
  buf : array [0..1024] of widechar;
  txtresult : array [0..1024] of char;
  result : string;
  comp,temp : array [0..100] of char;
  computer, tempfolder : string;
begin
  getenvironmentvariable('COMPUTERNAME',comp,100);
  computer := strpas(comp);
  getenvironmentvariable('TEMP',temp,100);
  tempfolder := strpas(temp);
  filemode := 0;
  assignfile(Unifile,'C:\program files\ukcbin\'+computer+'.wmsd.txt');
  reset(unifile, 1);
  filemode := 2;
  Assignfile(txtfile,temp+'\'+computer+'.txt');
  rewrite(txtfile);
   repeat
    blockread(unifile,Buf, SizeOf(Buf), NumRead);
    result := WideCharToString(buf);
    strpcopy(txtresult,result);
    write(txtfile,result);
  until (NumRead = 0);
  closefile(unifile);
  closefile(txtfile);
   close;
end;

Martin Searle
UKC, Computing Service

--
--
Martin Searle BSC(hons) Comp. Sci.,
Computing Officer (Computing)
Room S18, Cornwallis South, x7884
RYA Club Racing Coach

Other Threads