URIにASCIIの非予約文字以外の文字データを用いる場合には、「%xx」(xxは16進数)という形でコードを表記することが定められている。...
たとえば「ウィキペディア」を、各種の文字コードを用いてURLエンコードすると以下のようになる。
Shift_JIS:
%83E%83B%83L%83y%83f%83B%83A
EUC-JP:
%A5%A6%A5%A3%A5%AD%A5%DA%A5%C7%A5%A3%A5%A2
UTF-8:
%E3%82%A6%E3%82%A3%E3%82%AD%E3%83%9A%E3%83%87%E3%82%A3%E3%82%A2
Shift_JISのところがなんかへんですが、理由はこういうことらしいです。↓
各バイトがASCIIの非予約文字に対応するなら、その文字をそのまま使用しても良い。
参考)漢字のURLエンコードが%の3桁になります!
http://www.tohoho-web.com/lng/200001/00010160.htm
コマンド引数で指定した文字列を1バイトずつとりだし、16進数に変換するスクリプト
(my_encode.pl)
#!/usr/bin/env perl use Encode; $sjis = $ARGV[0]; $euc = $ARGV[0]; $utf8 = $ARGV[0]; Encode::from_to($sjis,"utf8","sjis"); Encode::from_to($euc, "utf8","euc-jp"); @array_sjis = split(//,$sjis); @array_euc = split(//,$euc); @array_utf8 = split(//,$utf8); print "shift-jis: "; foreach $char (@array_sjis){ print unpack("H2",$char)," "; } print "\n"; print "euc-jp : "; foreach $char (@array_euc){ print unpack("H2",$char)," "; } print "\n"; print "utf-8 : "; foreach $char (@array_utf8){ print unpack("H2",$char)," "; } print "\n";
実行結果
[tigerii@fedora ~]$ ./my_encode.pl ウィキペディア shift-jis: 83 45 83 42 83 4c 83 79 83 66 83 42 83 41 euc-jp : a5 a6 a5 a3 a5 ad a5 da a5 c7 a5 a3 a5 a2 utf-8 : e3 82 a6 e3 82 a3 e3 82 ad e3 83 9a e3 83 87 e3 82 a3 e3 82 a2
参照したURL:
http://d.hatena.ne.jp/midori_kasugano/20090616/1245139124
http://mikeneko.creator.club.ne.jp/~lab/perl/numerical_transform/
rubyで書く
(my_encode.rb)
#!/usr/bin/env ruby require 'kconv' print "Shift-JIS: " ARGV[0].tosjis.each_byte{|char| print char.to_s(16)," " } print "\n" print "EUC-JP : " ARGV[0].toeuc.each_byte{|char| print char.to_s(16)," " } print "\n" print "UTF-8 : " ARGV[0].toutf8.each_byte{|char| print char.to_s(16)," " } print "\n"
実行結果
$ ./my_encode.rb ウィキペディア Shift-JIS: 83 45 83 42 83 4c 83 79 83 66 83 42 83 41 EUC-JP : a5 a6 a5 a3 a5 ad a5 da a5 c7 a5 a3 a5 a2 UTF-8 : e3 82 a6 e3 82 a3 e3 82 ad e3 83 9a e3 83 87 e3 82 a3 e3 82 a2
参照したURL:
http://www.mapee.jp/ruby/rubyto_s281610.html
http://www.ruby-lang.org/ja/man/html/Kconv.html
gaucheノート
[tigerii@fedora ~]$ rlwrap gosh gosh> (define p (open-input-string "ウィキ")) p gosh> (read p) ウィキ gosh> (read p) #<eof> gosh> (define p (open-input-string "ウィキ")) p gosh> (read-char p) #\ウ gosh> (read-char p) #\ィ gosh> (read-char p) #\キ gosh> (read-char p) #<eof> gosh> (define p (open-input-string "ウィキ")) p gosh> (read-byte p) 227 gosh> (read-byte p) 130 gosh> (read-byte p) 166 gosh> (read-byte p) 227 gosh> (read-byte p) 130 gosh> (read-byte p) 163 gosh> (read-byte p) 227 gosh> (read-byte p) 130 gosh> (read-byte p) 173 gosh> (read-byte p) #<eof> gosh> (define (dec2hex num) (format #f "~X" num)) dec2hex gosh> (define p (open-input-string "ウィキ")) p gosh> (dec2hex (read-byte p)) "e3" gosh> (dec2hex (read-byte p)) "82" gosh> (dec2hex (read-byte p)) "a6" gosh> (dec2hex (read-byte p)) "e3" gosh> (dec2hex (read-byte p)) "82" gosh> (dec2hex (read-byte p)) "a3" gosh> (dec2hex (read-byte p)) "e3" gosh> (dec2hex (read-byte p)) "82" gosh> (dec2hex (read-byte p)) "ad" gosh> (dec2hex (read-byte p)) *** ERROR: number required, but got #<eof> Stack Trace: _______________________________________
参照したURL:
http://www.geocities.jp/m_hiroi/func/abcscm07.html
http://blog.livedoor.jp/mocoh/archives/568182.html