国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
(ok)Python3編碼問(wèn)題 Unicode utf

為什么需要本文,因?yàn)樵趯?duì)接某些很老的接口的時(shí)候,需要傳遞過(guò)去的是16進(jìn)制的hex字符串,并且要求對(duì)傳的字符串做編碼,這里就介紹了utf-8 Unicode bytes 等等。

#英文使用utf-8 轉(zhuǎn)換成16進(jìn)制hex字符串的方法newstr = 'asd'b_str = bytes(newstr,encoding='utf-8')print(b_str)hex_str = b_str.hex() #將bytes類型轉(zhuǎn)換成16進(jìn)制的hex字符串print(hex_str) #字節(jié)碼轉(zhuǎn)16進(jìn)制hex的方法print(bytes.fromhex(hex_str).decode('utf-8'))  #將16進(jìn)制hex字符串轉(zhuǎn)換成bytes,然后在轉(zhuǎn)換成字符串print(type('中文'.encode('utf-8')),'中文'.encode('unicode_escape'),'中文123456'.encode('unicode_escape').decode('utf-8'))#中文轉(zhuǎn)換成Unicode的一種方法之一u_str = '中文123456'b_str = bytes(u_str,encoding='unicode_escape')h_u_s = b_str.hex()print ("\u4e2d\u6587") #Unicode編碼可直接輸出#中文使用Unicode轉(zhuǎn)換成bytes再轉(zhuǎn)換成16進(jìn)制hex方法 包含英文和數(shù)字u_cn = '中文asd123'hex_msg = bytes(u_cn,encoding='utf_16_be').hex() #這是特殊要求下最終的解決方案#注意在Python3中已經(jīng)沒(méi)有了直接將字符串變成bytes或者Unicode的方法了#也就是說(shuō),在Python中 u'中文'已經(jīng)不再奏效#bytes轉(zhuǎn)strb_str = bytes('中文',encoding='utf-8')print(b_str.decode())  #直接輸出為普通字符串
  • /////////////////////////////////////

  • #英文使用utf-8 轉(zhuǎn)換成16進(jìn)制hex字符串的方法

  • newstr = 'asd'

  • b_str = bytes(newstr,encoding='utf-8')

  • print(b_str)

  • hex_str = b_str.hex() #將bytes類型轉(zhuǎn)換成16進(jìn)制的hex字符串

  • print(hex_str) #字節(jié)碼轉(zhuǎn)16進(jìn)制hex的方法

  • print(bytes.fromhex(hex_str).decode('utf-8'))  #將16進(jìn)制hex字符串轉(zhuǎn)換成bytes,然后在轉(zhuǎn)換成字符串

  • print(type('中文'.encode('utf-8')),'中文'.encode('unicode_escape'),'中文123456'.encode('unicode_escape').decode('utf-8'))

  • #中文轉(zhuǎn)換成Unicode的一種方法之一

  • u_str = '中文123456'

  • b_str = bytes(u_str,encoding='unicode_escape')

  • h_u_s = b_str.hex()

  • print ("\u4e2d\u6587") #Unicode編碼可直接輸出

  • #中文使用Unicode轉(zhuǎn)換成bytes再轉(zhuǎn)換成16進(jìn)制hex方法 包含英文和數(shù)字

  • u_cn = '中文asd123'

  • hex_msg = bytes(u_cn,encoding='utf_16_be').hex() 

  • #這是特殊要求下最終的解決方案

  • #注意在Python3中已經(jīng)沒(méi)有了直接將字符串變成bytes或者Unicode的方法了

  • #也就是說(shuō),在Python中 u'中文'已經(jīng)不再奏效

  • #bytes轉(zhuǎn)str

  • b_str = bytes('中文',encoding='utf-8')

  • print(b_str.decode())  #直接輸出為普通字符串


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
第15關(guān)筆記
python中的編碼問(wèn)題:以ascii和unicode為主線
Defining Python Source Code Encodings
字符串加密之后在解密
python2和3編碼
Python2到Python3的奇葩字符串問(wèn)題,小心踩坑哦
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服