網(wǎng)站首頁 編程語言 正文
binascii模塊用法
binascii模塊用于在二進(jìn)制和ASCII之間轉(zhuǎn)換
>> import binascii # 將binary 轉(zhuǎn)ascii并用十六進(jìn)制表示 >> str1 = b"hello world" ? >> binascii.b2a_hex(b"hello world") # 輸出 b'68656c6c6f20776f726c64' # 相反操作 >> binascii.a2b_hex(b'68656c6c6f20776f726c64') # 輸出b'hello world' >> binascii.hexlify(b"hello world") ? ? # 注解: 同b2a_hex(), 返回二進(jìn)制 data 的十六進(jìn)制表示。 data 的每個(gè)字節(jié)都轉(zhuǎn)換為相應(yīng)的2位十六進(jìn)制表示。因此返回的字節(jié)對(duì)象的長度是 data 的長度的兩倍。 # 輸出 b'68656c6c6f20776f726c64' >> binascii.unhexlify(b'68656c6c6f20776f726c64') ? ? # 注解: 同a2b_hex(), 返回由十六進(jìn)制字符串 hexstr 表示的二進(jìn)制數(shù)據(jù)。 hexstr 必須包含偶數(shù)個(gè)十六進(jìn)制數(shù)字(可以是大寫或小寫),否則會(huì)引發(fā) Error 異常。 # 輸出b'hello world'
binascii模塊和進(jìn)制轉(zhuǎn)換筆記
廢話少說,直接上代碼:
# !/usr/bin/env python # -*- coding:utf-8 -*- # author:大西瓜 ? # 導(dǎo)入binascii模塊 import binascii ? a = b'BE27E8FFFF010203' # 先把b'BE27E8FFFF010203'轉(zhuǎn)換成二進(jìn)制數(shù)據(jù)然后在用十六進(jìn)制表示 b = binascii.b2a_hex(a) # 打印出:b'42453237453846464646303130323033',例如B對(duì)應(yīng)ascii碼42,E對(duì)應(yīng)ascii碼45 print(b) ? ? # 與b2a_hex相反,打印出:b'BE27E8FFFF010203' print(binascii.a2b_hex(b)) ? ? # 這個(gè)功能和b2a_hex()一樣 # 打印出:b'42453237453846464646303130323033',例如B對(duì)應(yīng)ascii碼42,E對(duì)應(yīng)ascii碼45 c = binascii.hexlify(a) print(c) ? # 這個(gè)功能和a2b_hex()一樣,打印出:b'BE27E8FFFF010203' print(binascii.unhexlify(c))
?Python內(nèi)置函數(shù)
-
hex()
:十進(jìn)制轉(zhuǎn)十六進(jìn)制
#把10進(jìn)制轉(zhuǎn)整形換成16進(jìn)制 >>> hex(88) '0x58' #把浮點(diǎn)型轉(zhuǎn)換成16進(jìn)制 >>> 1.23.hex() '0x1.3ae147ae147aep+0' #內(nèi)置函數(shù)hex和binascii.hexlify()的區(qū)別就在于, #hex只能接受整形不能接受字符串 >>> hex('88') Traceback (most recent call last): ? File "<pyshell#26>", line 1, in <module> ? ? hex('88') TypeError: hex() argument can't be converted to hexbin():ba
-
bin()
:把十進(jìn)制整形轉(zhuǎn)換成二進(jìn)制字符
#把十進(jìn)制整型轉(zhuǎn)換成二進(jìn)制 >>> bin(88) '0b1011000' >>> bin(33) '0b100001' oct():把十進(jìn)制轉(zhuǎn)換成八進(jìn)制字符 #把十進(jìn)制轉(zhuǎn)換成八進(jìn)制 >>> oct(500) '0764' >>> oct(488) '0750'
-
chr()
:把一個(gè)整形轉(zhuǎn)換成ASCII碼表中對(duì)應(yīng)的單個(gè)字符
#把一個(gè)整形轉(zhuǎn)換成ASCII碼表中對(duì)應(yīng)的單個(gè)字符 >>> chr(98) 'b' >>> chr(97) 'a' ord():和chr相反,把ASCII碼表中的字符轉(zhuǎn)換成對(duì)應(yīng)的整形 >>> ord('b') 98 >>> ord('c') 99
原文鏈接:https://blog.csdn.net/u011361138/article/details/82428403
相關(guān)推薦
- 2022-03-26 docker?registry?私有倉庫的搭建過程_docker
- 2022-04-11 css左側(cè) div給固定寬 右側(cè)div自適應(yīng)
- 2023-02-12 python中使用docx模塊處理word文檔_python
- 2023-03-28 通知監(jiān)控NotificationListenerService?onNotificationPost
- 2022-09-25 線性回歸的從零開始實(shí)現(xiàn)(線性神經(jīng)網(wǎng)絡(luò))
- 2022-08-20 Linux中sftp常用命令整理_linux shell
- 2022-03-30 .NET?Core使用EF生成數(shù)據(jù)庫出錯(cuò)的解決方法_實(shí)用技巧
- 2022-12-06 Python+Pygame實(shí)現(xiàn)代碼雨動(dòng)畫效果_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支