Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion How do I open a text file with Unicode encoding? (Should've asked this before.)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peter Below (TeamB)  
View profile  
 More options Aug 16 2000, 3:00 am
Newsgroups: borland.public.delphi.winapi
From: "Peter Below (TeamB)" <100113.1...@compuXXserve.com>
Date: 2000/08/16
Subject: Re: How do I open a text file with Unicode encoding? (Should've asked this before.)

In article <399994eb_2@dnews>, Samuel A. Winchenbach wrote:
> P.S.  Can anyone (read Peter Below) tell me where the ByteOrderMark := 65279
> comes from?

It is the recommended way to start a unicode text file, and documented in
win32.hlp (topic "Byte-order Mark"). Its purpose is to handle UNICODE files
written on other platforms that use a different byte order (big-endian instead
of the Intel little-endian). If you read the first word of a UNICODE file and
see that it is $FFFE instead of $FEFF you know that you have to swap the bytes
in each word you read to get a valid Widechar for your platform. Delphi has a
Swap function that performs this byte order switch.

So if you read a UNICODE file you have to be prepared to deal with files that
have the correct byte order mark for your platform, that need to be swapped
and those that do not have a byte order mark at all (since it is recommended
but not enforcible, of course).

Your routine could be modified to deal with this like follows (untested):

Procedure SwapBytesInWideString( Var ws: WideString );
var
  i: Integer;
begin
  for i:= 1 to Length( ws ) do
    ws[i] := Swap( ws[i] );
    // if compiler balks at this try
    // ws[i] := WideChar( Swap( word( ws[i] )));
end;

procedure TMainForm.Open1Click(Sender: TObject);
var
  ws : WideString;
  fs : TFileStream;
begin
  If OpenDialog1.Execute then
  begin
    fs:=TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
    try
      SetLength( ws, fs.size div 2 );
      fs.ReadBuffer( ws[1], fs.Size );
      If ws[1] = #$FFFE Then
        SwapBytesInWideString( ws );
      If ws[1] = #$FEFF Then
        Delete( ws, 1, 1 );
      RichEdit1.Text:= ws;
    finally
      fs.free
    end;
  end;
end;

Peter Below (TeamB)  100113.1...@compuserve.com)
No replies in private e-mail, please, unless explicitly requested!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.