Einzelnen Beitrag anzeigen

mjustin

Registriert seit: 14. Apr 2008
3.005 Beiträge
 
Delphi 2009 Professional
 
#1

Text von DOS/Windows Codepage nach Unicode konvertieren (auch unter Linux)

  Alt 16. Jan 2023, 18:51
Wie konvertiere ich in Free Pascal (3.x) unter Linux einen Text, der in einem bekannten DOS- oder Windows Encoding vorliegt, unter Angabe einer Single- oder Multibyte Codepage (z.B. Windows-1252, DOS 437/850, 949 (Koreanisch) ...))? Unter Windows funktioniert es mittels MultiByteToWideChar. Die Strings könnten eine dieser Codepages verwenden. Welche es ist, ist zum Konvertierungszeitpunkt bekannt. (z.B. CP 1258, Vietnamesisch, oder CP 932, Japanisch SJIS)

Delphi-Quellcode:
  // code page ids
  CP_THAI = 874; // ANSI/OEM Thai (ISO 8859-11); Thai (Windows)
  CP_SJIS = 932; // ANSI/OEM Japanese; Japanese (Shift-JIS)
  CP_GB2312 = 936; // ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312)
  CP_KOREAN = 949; // ANSI/OEM Korean (Unified Hangul Code)
  CP_BIG5 = 950; // ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5)
  CP_EASTEUROPE = 1250; // ANSI Central European; Central European (Windows)
  CP_RUSSIAN = 1251; // ANSI Cyrillic; Cyrillic (Windows)
  CP_ANSI = 1252; // ANSI Latin 1; Western European (Windows)
  CP_GREEK = 1253; // ANSI Greek; Greek (Windows)
  CP_TURKISH = 1254; // ANSI Turkish; Turkish (Windows)
  CP_HEBREW = 1255; // ANSI Hebrew; Hebrew (Windows)
  CP_ARABIC = 1256; // ANSI Arabic; Arabic (Windows)
  CP_BALTIC = 1257; // ANSI Baltic; Baltic (Windows)
  CP_VIETNAMESE = 1258; // ANSI/OEM Vietnamese; Vietnamese (Windows)
  CP_MACROMAN = 10000; // MAC Roman; Western European (Mac)
In der Dokumentation unter https://www.freepascal.org/docs-html...x32-390003.2.4 ist dieses Beispiel enthalten:

Delphi-Quellcode:
{$h+}  
uses sysutils;

Type
  TString1 = Type*String(1252);
  TString2 = Type*String(1251);

Var
  A : TString1;
  B : TString2;

begin
  A:='123'+'345'+intToStr(123);
  B:=A;
  Writeln('B: "',B,'" : ',StringRefCount(B),' -> ',StringCodePage(B));
  Writeln('A: "',A,'" : ',StringRefCount(A),' -> ',StringCodePage(A));
end.

Für Unices heisst es dazu: "Remark Code page support requires quite some helper routines, these are implemented in the unicodestring manager. On windows, the system routines are used for this. On Unices, the cwstring unit can be used to link to the C library and use the C library conversion support. Alternatively, the fpwidestring unit contains a unicodestring manager implemented natively in Object Pascal."

Ist dieser Lösungsansatz der Einzige (für Unices) oder gibt es Alternativen, die einfacher zu implementieren sind, und keine Deklaration der String-Typen für alle benötigten Codepages notwendig ist?


https://en.wikipedia.org/wiki/Windows_code_page
Michael Justin
  Mit Zitat antworten Zitat