2020-07-16

【Python】讀出 BIG5 中文的內碼

土法鍊鋼的做法:
  1.  
  2. #!/usr/bin/python
  3. #-*- coding:utf-8 -*-
  4.  
  5. # 因為程式碼本身是 utf-8, 所以先強制將字串轉碼成 BIG5
  6. strbig5 = "big 中文測試".decode('utf8','ignore').encode('big5','ignore')
  7.  
  8. ii = 0
  9. while ii<(len(strbig5)-1):
  10. bCH = False
  11. s1 = strbig5[ii]
  12. s2 = ""
  13. if (ord(s1)>127):
  14. bCH = True
  15. s2 = strbig5[ii+1]
  16. print (s1+s2).decode('big5','ignore').encode('utf8','ignore') + " 的 ASCII : " + hex(ord(s1)).strip('0x') + hex(ord(s2)).strip('0x') + "\n"
  17. else:
  18. print s1 + " 的 ASCII : " + hex(ord(s1)) + "\n"
  19. if bCH:
  20. ii += 1
  21. ii += 1
  22.  

沒有留言:

張貼留言