this slowpoke moves

Convert Unicode

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Font.Handle := GetStockObject(OEM_FIXED_FONT);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
var
  ch: string[1];
begin
  CharToOem(@Key, @ch[1]);
  Key := ch[1];
end;

// Memo to Ansi
procedure ConvertANSIFileToUTF8File(AInputFileName, AOutputFileName: TFileName);
var Strings: TStrings;
begin
  Strings := TStringList.Create; try
    Strings.LoadFromFile(AInputFileName);
    Strings.Text := UTF8Encode(Strings.Text);
    Strings.SaveToFile(AOutputFileName);
  finally Strings.Free; end;
end;

// Memo to UTF-8
procedure TForm1.Button1Click(Sender: TObject);
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
    Strings.Text := UTF8Encode(Memo1.Text);
   Strings.SaveToFile('UTF-8.txt');
end;

// Memo to UTF-8 Boom
procedure TForm1.Button2Click(Sender: TObject);
const
  UTF8BOM: array[0..2] of Byte = ($EF, $BB, $BF);
var
  UTF8Str: UTF8String;
  FS: TFileStream;
begin
  UTF8Str := UTF8Encode(Memo1.Text);
  FS := TFileStream.Create('UTF-8 Boom.txt', fmCreate);
  try
    FS.WriteBuffer(UTF8BOM[0], SizeOf(UTF8BOM));
    FS.WriteBuffer(PAnsiChar(UTF8Str)^, Length(UTF8Str));
  finally
    FS.Free;
  end;
end;

// Memo to UTF-16 LE
procedure TForm1.Button3Click(Sender: TObject);
var
  BOM: WideChar;
  FS: TFileStream;
  WS: WideString;
  I: Integer;
begin
  FS := TFileStream.Create('UTF-16 LE.txt', fmCreate);
  try
    BOM := WideChar($FEFF);
    FS.WriteBuffer(BOM, SizeOf(BOM));
    For I := 0 to Memo1.Lines.Count-1 do
    begin
      WS := WideString(Memo1.Lines[I] + sLineBreak);
      FS.WriteBuffer(PWideChar(WS)^, Length(WS) * SizeOf(WideChar));
    end;
  finally
    FS.Free;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate