0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Shift-JIS(CP932)を UTF-8 に変換する

Last updated at Posted at 2016-02-12

Lazarus で作成したアプリを Windows 上で動作させる前提で、
Shift-JIS 文字コードを UTF-8 に変換するには、システム文字コードからの UTF-8 変換だけで済んでしまう。
例えば、テキストファイルを読み込みする場合、テキストファイル内の文字コードが Shift-JIS で記載されていて、
自作アプリ内でファイルの内容を操作したいなど。

例 システム文字コードからの UTF-8 変換

FPC2.6系のWindowsだとシステム文字コードは、 Shift-JIS と位置付けられているようだ。

var
  sjisString: String;
  utf8String: String;
begin
       :
  utf8String := SysToUTF8(sjisString)
       :
end;

但し、この方法だと、Mac 上(OSX)では、コード変換が意図した動きにはならない。
Mac(OSX)だと、システム文字コードが UTF-8 に位置付けられているようだ。
そこで、文字コードページを指定して変換する方法がないかをググッてみたけど、中々見つからない。

Shift-JIS のコードページは、CP932。
ググッても、ダメだったので、Lazarus の便利関数内に良さそうな処理がないかを探してみる。
見つかったのは、LConvEncoding というユニット。
/Developer/lazarus/components/lazutils/lconvencoding.pas

例 LConvEncoding を使用してみた

var
  sjisString: String;
  utf8String: String;
begin
  // uses LConvEncoding
       :
  utf8String := CP932ToUTF8(sjisString)
       :
end;

CP932ToUTF8 関数だと、
Mac(OSX)上でもShift-JIS(CP932)から UTF-8 に変換できたので、ちょっと、様子を見てみる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?