miau's blog?

コンソール(cmd.exe)の文字コードを UTF-8 に

最近だとソースコードや DB を UTF-8 で統一するのが当たり前になってきてますが、日本語版の Windows は cmd.exe で Shift_JIS(Windows-31J)以外でエンコードされた文字を出力すると文字化けしてしまいます。

この対応について、半端ではありますがいくつか調べたのでそのお話。




■対応方法

○chcp 使う→これだけではダメ。

去年の頭の時点で一般的にいわれていた対策は「chcp 65001」にするというもの。

chcp 65001

でもこれってうまくいかないんですよね。「これは UTF-8 ですよ。」という内容のテキストファイルを出力すると、こんな感じ。

chcp_bake1
(ラスタフォント。マルチバイトが 1 バイトずつ認識されてる。)

chcp_bake2
(Lucida Console。文字認識はうまくいってるけどフォントが該当文字を持っていない。)

chcp_bake3
(Win7 上でのラスタフォント。元々 3 バイトなのに 2 バイトになってる???)

さらに環境によっては dir で日本語表示したときに cmd.exe が落ちることも。

○フォントを指定する

chcp だけではダメで、フォントも指定する必要があるそうで。その指定も色々試して失敗してたんだけど、どうもこの方法が一番まともに動作する様子。

へっぽこSE奮闘記 - コマンドプロンプトのフォントを変更する方法

REG コマンドでやるなら、こんな感じ。

reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont" /v "0." /t "REG_SZ" /d "MS Gothic"

chcp 65001 の状態でフォントを指定してしまっていると反映されないので、コマンドプロンプトを一旦閉じたり、再起動したりするといいかもしれません。

○残る問題点

そもそも cmd.exe の動作が不安定なので UTF-8 を使う場合特有の問題かどうかもわからないのですが、文字の描画位置がずれたりもします。この現象については以下のページが詳しいです。

コマンドプロンプトの謎 - ××××Diary

■レジストリ設定の意味は?

○疑問

それにしても、

値の名前:0. (ゼロ ドット)

ってどういう意味?「こうすればできますよ」って情報はそれだけでも嬉しいんだけど、理屈がわからないと気持ち悪い。

○公式情報

ということで調べてみると・・・

Necessary criteria for fonts to be available in a command window

MS 公式では、「コンソールで使えるフォントの選択肢を増やすには 00、000 と 0 を増やして項目を追加してね」って書いてある。(その他指定可能なフォントについても細かい情報あり。)ドットってどこから出てきたの?

○オープンソース万歳

そういえば Windows 2000 の流出コードってあったなー、ということでちょっとのぞいてみる。

断片的なコード(定数の定義が見つからないこともあったり)だし、どういうタイミングで呼ばれる処理かもわからないけど、

・private\shell\shell32\dbcs.c
 →上記のレジストリキーが定数定義されている
・private\ntos\w32\ntcon\server\dbcs.c
 →該当キーの操作をしている箇所あり
・private\windows\shell\control\console\dbcs.c
 →似たような処理があるけど 16 bit 版?

という感じ。

コードページの変換には簡易版の atoi っぽい自作関数が使われていて、
・文字列を先頭から走査
・数字([0-9])であればバッファを x 10 してその値を加算
・数字以外が来たら終了
というような処理になってる。このロジックで 0 と判定される文字列だったらなんでもいいので、「0.」でも「00」でもいいし、おそらく「0hoge」なんかでもうまくいくんでしょう。

○どうするのがベスト?

こちらの方が書いているように、ほかのコードページについては、.1、.2 のように増やしていくのが見やすい気がします。

cmd.exe(コマンド プロンプト)のフォントを変更する | Windows - P-SOC

ちなみに reg コマンドもこの方のページを参考にしました。

cmd.exe(コマンド プロンプト)のフォントを変更する(reg コマンド編) | Windows - P-SOC

■その他試したけどダメだったこと

それぞれの反映のタイミングがよくわからない&なにか設定がキャッシュされている様子なので、検証方法が悪いだけかもしれませんが・・・以下ではダメでした。

○UTF-8 のフォントを指定する

reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont" /v "65001" /t "REG_SZ" /d "MS Gothic"

→結局一覧に出てこないので意味がない?

○デフォルトである Lucida Console を変えてみる

reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont" /v "0" /t "REG_SZ" /d "MS Gothic"

→候補から Lucida Console が消えただけ。この方法でうまくいったこともあるんだけど、\ がウォン表記になってたりで死ぬほど怪しかったり。

○「MS Gothic」を「MS ゴシック」だとか「*MS ゴシック」に変えてみる

reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont"

の一覧に出てくるフォントを使うのが礼儀らしいけど、コードページ 932 や Web 上の情報では結構日本語表記になっていたので試してみたりも。
→効果なし。

■その他参考ページ等

INFO: SetConsoleOutputCP Only Effective with Unicode Fonts

ラスタフォントについては chcp は効かないとか書いてる気がする。あと Lucida Console しか使えないとか??

コマンドプロンプトの文字コードを変更する。 - 趣味でやってます。

コードページの一覧あり。EUC-JP は chcp 20932、ISO-2022-JP(JIS)は chcp 50222 or 50220 ってところ。

コマンドプロンプトでUTF-8の文字を表示する。 - Perl入門〜サンプルコードによるPerl入門〜

cmd.exe の起動時に chcp を呼び出してオプションで文字コード指定する方法。常に UTF-8 を使うようなケースでは役に立つかも。


その他フォントリンクで解決する方法もある気がする。ラスタフォントには指定できないけど、Lucida Console には設定できるかも?
posted at 07:06:53 on 2009-03-23 by miau - Category: General No Trackbacks - Permalink

TrackBack

このエントリにトラックバックはありません
このトラックバックURLを使ってこの記事にトラックバックを送ることができます。 もしあなたのブログがトラックバック送信に対応していない場合にはこちらのフォームからトラックバックを送信することができます。.

Comments

dell akku wrote:

I thank you for this information. Really well put. Thanks once again!
2011-08-29 17:50:37

Coach factory outlet online wrote:

Coach factory outlet
2011-09-03 11:02:13

___ ______ wrote:

Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much.
I hope to give something back and aid others like you aided me.
2012-07-21 20:10:39

LV Outlet wrote:

Excellent weblog, many thanks a lot for your awesome posts!
2012-09-11 04:14:14

Isabel Marant Wedge Sneakers wrote:

http://www.49erstime.com/ Nike 49ers Jersey
http://www.49erstime.com/19... Patrick Willis Jersey
http://www.49erstime.com/10... Frank Gore Jersey
http://www.49erstime.com/13... Justin Smith Jersey
http://www.49erstime.com/26... Alex Smith Jersey
http://www.49erstime.com/23... Vernon Davis Jersey
http://www.49erstime.com/12... Joe Staley Jersey
http://www.49erstime.com/56... Carlos Rogers Jersey
http://www.49erstime.com/18... NaVorro Bowman Jersey
http://www.49erstime.com/8-... A.J. Jenkins Jersey
http://www.49erstime.com/14... Ahmad Brooks Jersey
http://www.49erstime.com/20... Aldon Smith Jersey
http://www.49erstime.com/32... Andy Lee Jersey
http://www.49erstime.com/38... Anthony Davis Jersey
http://www.49erstime.com/44... Anthony Dixon Jersey
http://www.49erstime.com/50... Brandon Jacobs Jersey
http://www.49erstime.com/62... Chris Culliver Jersey
http://www.49erstime.com/68... Chris Owusu Jersey
http://www.49erstime.com/74... Colin Kaepernick Jersey
http://www.49erstime.com/80... Dashon Goldson Jersey
http://www.49erstime.com/86... David Akers Jersey
http://www.49erstime.com/92... Deion Sanders Jersey
http://www.49erstime.com/93... Delanie Walker Jersey
http://www.49erstime.com/99... Donte Whitner Jersey
http://www.49erstime.com/11... Isaac Sopoaga Jersey
http://www.49erstime.com/11... Jerry Rice Jersey
http://www.49erstime.com/12... Joe Montana Jersey
http://www.49erstime.com/13... Jonathan Goodwin Jersey
http://www.49erstime.com/14... Kendall Hunter Jersey
http://www.49erstime.com/14... Kyle Williams Jersey
http://www.49erstime.com/15... LaMichael James Jersey
http://www.49erstime.com/16... Larry Grant Jersey
http://www.49erstime.com/16... Mario Manningham Jersey
http://www.49erstime.com/17... Michael Crabtree Jersey
http://www.49erstime.com/17... Mike Iupati Jersey
http://www.49erstime.com/19... Parys Haralson Jersey
http://www.49erstime.com/20... Randy Moss Jersey
http://www.49erstime.com/20... Ray McDonald Jersey
http://www.49erstime.com/21... Roger Craig Jersey
http://www.49erstime.com/21... Ronnie Lott Jersey
http://www.49erstime.com/21... Scott Tolzien Jersey
http://www.49erstime.com/22... Steve Young Jersey
http://www.49erstime.com/22... Tarell Brown Jersey
http://www.49erstime.com/23... Ted Ginn Jersey
http://www.49erstime.com/24... Y.A. Tittle Jersey
2012-09-25 03:23:01

Brandon Marshall Jersey wrote:

http://www.teamfanaticsgo.com/
http://www.teamfanaticsgo.c... Brandon Marshall Jersey
http://www.teamfanaticsgo.c... Brian Urlacher Jersey
http://www.teamfanaticsgo.c... Jay Cutler Jersey
http://www.teamfanaticsgo.c... Julius Peppers Jersey
http://www.teamfanaticsgo.c... Roddy White Jersey
http://www.teamfanaticsgo.c... Tony Gonzalez Jersey
http://www.teamfanaticsgo.c... Andre Johnson Jersey
http://www.teamfanaticsgo.c... J.J. Watt Jersey
http://www.teamfanaticsgo.c... Matt Schaub Jersey
http://www.teamfanaticsgo.c... Torrey Smith Jersey
http://www.teamfanaticsgo.c... Terrell Suggs Jersey
http://www.teamfanaticsgo.c... Peyton Manning Jersey
http://www.teamfanaticsgo.c... Aaron Rodgers Jersey
http://www.teamfanaticsgo.c... Clay Matthews Jersey
http://www.teamfanaticsgo.c... Rob Gronkowski Jersey
http://www.teamfanaticsgo.c... Tom Brady Jersey
http://www.teamfanaticsgo.c... Eli Manning Jersey
http://www.teamfanaticsgo.c... Hakeem Nicks Jersey
http://www.teamfanaticsgo.c... Jason Pierre-Paul Jersey
http://www.teamfanaticsgo.c... Victor Cruz Jersey
http://www.teamfanaticsgo.c... Robert Griffin III Jersey
http://www.teamfanaticsgo.c... Alex Smith Jersey
http://www.teamfanaticsgo.c... Frank Gore Jersey
http://www.teamfanaticsgo.c... Patrick Willis Jersey
http://www.teamfanaticsgo.c... Cam Newton Jersey
http://www.teamfanaticsgo.c... DeMarcus Ware Jersey
http://www.teamfanaticsgo.c... Emmitt Smith Jersey
http://www.teamfanaticsgo.c... Jason Witten Jersey
http://www.teamfanaticsgo.c... Russell Wilson Jersey
2012-11-17 15:37:54

moneyquickcash wrote:

Thank you a bunch for sharing this with all folks you actually realize what you're talking approximately! Bookmarked. Please additionally discuss with my site =). We can have a link trade contract among us
2012-11-28 10:11:20

Randy Moss Jersey wrote:

My brother recommended I might like this Frank Gore Jersey web
site. He was entirely right. This post actually made my day.
You cann't imagine Randy Moss Jersey just how much time I had spent for this info! Thanks!
2012-12-10 06:30:21

http://wealthwayonline.com/ wrote:

Howdy, I do think your blog could be having internet browser compatibility issues.
Whenever I take a look at your web site in Safari, it looks fine
however when opening in I.E., it's got some overlapping issues. I simply wanted to give you a quick heads up! Other than that, fantastic website!
2013-04-24 21:35:20

Chaussure air max wrote:

Quality content is the key to interest the viewers to go to
see the site, that's what this web page is providing.
2013-04-25 05:59:33

http://www.converseshopfr.com/ wrote:

I like what you guys are usually up too. This type of clever work and reporting!
Keep up the amazing works guys I've added you guys to our blogroll.
2013-04-29 11:10:51

Nike Air Max Classic BW wrote:

I have been browsing online greater than three hours
these days, but I by no means discovered any attention-grabbing article like
yours. It's lovely value enough for me. In my view, if all website owners and bloggers made just right content as you probably did, the internet will be a lot more useful than ever before.
2013-05-01 11:40:21

Nike Air Max wrote:

Hey just wanted to give you a quick heads up. The
text in your post seem to be running off the screen in Safari.

I'm not sure if this is a format issue or something to do with internet browser compatibility but I figured I'd post to let you know.
The layout look great though! Hope you get the issue solved
soon. Cheers
2013-05-06 08:26:19

Air Max wrote:

Your means of telling all in this article is in fact
nice, all be capable of easily know it, Thanks a lot.
2013-05-07 16:16:13

Add Comments