2007. január 7., vasárnap

Determine if a Unicode string is Baltic or Russian


Problem/Question/Abstract:

How to determine if a Unicode string is Baltic or Russian

Answer:

procedure TForm1.SetReadableText(const ws: WideString);
var
  s: string;
  pch: PChar;
  i, CodePage, Charset: Integer;
begin
  CodePage := 1252;
  Charset := ANSI_CHARSET;
  pch := PChar(PWideChar(ws));
  for i := 0 to length(ws) - 1 do
  begin
    if ord(pch[2 * i + 1]) = 1 then
    begin
      CodePage := 1257;
      Charset := BALTIC_CHARSET;
      break;
    end;
    if ord(pch[2 * i + 1]) = 4 then
    begin
      CodePage := 1251;
      Charset := RUSSIAN_CHARSET;
      break;
    end;
  end;
  setlength(s, 2 * length(ws));
  setlength(s, WideCharToMultiByte(CodePage, 0, PWideChar(ws), length(ws),
    PChar(s), length(s), nil, nil));
  Edit1.Font.Charset := Charset;
  Edit1.Text := s;
end;

Nincsenek megjegyzések:

Megjegyzés küldése