LoginSignup
1
1

More than 5 years have passed since last update.

Python3で漢字の文字コードを取得する

Posted at

漢字の文字コードが知りたくなる時、皆さんありますよね。文字コードなんてネットで検索すればもちろんいくらでも出てくる情報ですが、今回はPython3上で漢字の文字からUnicode, Shift_JIS, EUC-JP, JISコード (iso-2022-jp), Big5の各文字コードを取得する方法です。例として漢字の「一」に対応する文字コードを取り出します。

Python3
print("Unicode: {}".format(
    hex(ord("一"))[2:].upper()
))
# Unicode: 4E00

print("Shift_JIS: {}".format(
    hex(int.from_bytes("一".encode("shift_jis"), "big"))[2:].upper()
))
# Shift_JIS: 88EA

print("EUC-JP: {}".format(
    hex(int.from_bytes("一".encode("euc-jp"), "big"))[2:].upper()
))
# EUC-JP: B0EC

print("JISコード (iso-2022-jp): {}".format(
    (hex("一".encode("iso-2022-jp")[3])[2:]+hex("一".encode("iso-2022-jp")[4])[2:]).upper()
))
# JISコード (iso-2022-jp): 306C

print("Big5: {}".format(
    "".join([hex(x)[2:] for x in "一".encode("big5")]).upper()
))
# Big5: A440

環境に依存する可能性がありますが、Ideone.comで正しく動くことを確認しました( https://ideone.com/PEeAdx )。

1
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
1
1