{"id":13,"date":"2012-10-06T10:44:36","date_gmt":"2012-10-06T08:44:36","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=13"},"modified":"2012-10-09T09:21:50","modified_gmt":"2012-10-09T07:21:50","slug":"ansitooem","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/06\/ansitooem\/","title":{"rendered":"ANSIToOEM"},"content":{"rendered":"<p>Today I found some old source code. Someone wanted to write a file in the old OEM codepage. Therefore he wrote a small function that encapsulates the Windows API CharToOem function.<\/p>\n<pre class=\"lang:delphi decode:true\">class function TStrUtil.ANSIToOEM(const ANSI: string): string;\r\nvar\r\n  sBuffer: AnsiString;\r\nbegin\r\n  if ANSI = '' then\r\n    Exit('');\r\n\r\n  SetLength(sBuffer, Length(ANSI));\r\n  if CharToOem(PChar(ANSI), PAnsiChar(sBuffer)) then\r\n    Result := string(sBuffer)\r\n  else\r\n    Result := ANSI;\r\nend;<\/pre>\n<p>After that he added the OEM string to a TStringList and saved the TStringList to a file.<\/p>\n<pre class=\"lang:delphi decode:true\">function TMyForm.SaveTheFile(const AFileName: string)\r\nvar\r\n  pList: TStringList;\r\nbegin\r\n  pList := TStringList.Create;\r\n  try\r\n    pList.Add(TStrUtils.ANSIToOEM('\u00c4\u00d6\u00dc'));\r\n    pList.SaveToFile(AFileName);\r\n  finally\r\n    pList.Free;\r\n  end;\r\nend;<\/pre>\n<p>I&#8217;m surprised that this code does work. Since Delphi 2010 strings are Unicode strings. This means that the OEM string is stored in a Unicode string variable.<\/p>\n<p>Instead, I think is is much easier to use the wonderful TEncoding class.<\/p>\n<pre class=\"lang:delphi decode:true\">function TMyForm.SaveTheFile(const AFileName: string)\r\nvar\r\n  pList: TStringList;\r\n  pOEMEncoding: TEncoding;\r\nbegin\r\n  pOEMEncoding := nil;\r\n  pList := TStringList.Create;\r\n  try\r\n    pOEMEncoding := TEncoding.GetEncoding(437);\r\n    pList.Add('\u00c4\u00d6\u00dc');\r\n    pList.SaveToFile(AFileName, pOEMEncoding);\r\n  finally\r\n    pList.Free;\r\n    pOEMEncoding.Free:\r\n  end;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I found some old source code. Someone wanted to write a file in the old OEM codepage. Therefore he wrote a small function that encapsulates the Windows API CharToOem function. class function TStrUtil.ANSIToOEM(const ANSI: string): string; var sBuffer: AnsiString; &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/06\/ansitooem\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":11,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}