日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

python 把int类型转bytes以及把bytes 转int 类型(使用方法to_bytes ,from_byte, struct)

發(fā)布時間:2023/11/27 生活经验 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 把int类型转bytes以及把bytes 转int 类型(使用方法to_bytes ,from_byte, struct) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

把int類型轉(zhuǎn)bytes

方法1 使用方法to_bytes

to_bytes 方法里面有3個參數(shù) ,

第一個數(shù)是指定要轉(zhuǎn)換的bytes占多少個字節(jié)

第二個是byteorder 是指定大端或者是小端 的

第三個是signed參數(shù)表示這個bytes對應(yīng)的是有符號的數(shù),或者無符號的int,這個是boolean值可以不寫

使用to_bytes把1 轉(zhuǎn)化了占2個字節(jié)的bytes ,并且指定大端

num_a = (1).to_bytes(2, "big")
# 或者
# num_a = (168).to_bytes(2, byteorder="big")
print(num_a)

打印結(jié)果

使用to_bytes把1 轉(zhuǎn)化了占2個字節(jié)的bytes ,并且指定小端

num_a = (1).to_bytes(2, "little")
# 或者
# num_a = (168).to_bytes(2, byteorder="little")
print(num_a)

打印結(jié)果:

使用struct

如果對struct 不了解可以點擊查看struct

使用struct把1 轉(zhuǎn)化了占2個字節(jié)的bytes ,并且指定大端

import structnum_a = struct.pack(">h", 1)
print(num_a)

打印結(jié)果

使用struct把1 轉(zhuǎn)化了占2個字節(jié)的bytes ,并且指定小端

import structnum_a = struct.pack("<h", 1)
print(num_a)

打印結(jié)果

把 bytes 轉(zhuǎn)?int類型

使用 from_bytes 和 unpack

import structprint(int.from_bytes(b'\x00\x01', "big"))
print(int.from_bytes(b'\x01\x00', "little"))
print(struct.unpack(">h", b'\x00\x01')[0])
print(struct.unpack("<h", b'\x01\x00')[0])

打印結(jié)果

總結(jié)

以上是生活随笔為你收集整理的python 把int类型转bytes以及把bytes 转int 类型(使用方法to_bytes ,from_byte, struct)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。