我有一些小endian編碼字節的文件,我想採取N
字節,指定endianess並使用python(任何版本)將它們轉換成十進制數。如何正確地做到這一點?如何將blob轉換爲Python中的整數?
回答
在Python 3,你可以使用這樣的事情:
int.from_bytes(byte_string, byteorder='little')
Ooooo我不知道。 +1 –
好的,我們有一個針對Python 3的解決方案。如何爲Python 2做到這一點? – warchantua
使用'struct'作爲@ juanpa.arrivillaga已經提到 – dunder
使用Python 3(或2),你可以用struct庫實現這一目標。
with open('blob.dat', 'rb') as f:
data = f.read(n)
現在,您使用合適的format specifier string進行解壓縮。例如,大端INT:
num = struct.unpack(">i",data)
'struct'也可以在Python2中使用,不是嗎? – VPfB
@VPfB是的。請參閱[文檔](https://docs.python.org/2.7/library/struct.html#module-struct)。 –
作爲Harshad Mulmuley的回答顯示,這是很容易在Python 3,使用int.from_bytes
方法。在Python 2中,這有點棘手。
struct
模塊旨在處理標準C數據類型。它不會處理任意長度的整數(Python 2 long
整數),因爲它們不是C本地的,但可以使用簡單的for
循環來轉換它們。我預計這將比Python 3更慢,因爲Python for
循環比C速度循環要慢,就像int.from_bytes
(可能)那樣。
from binascii import hexlify
def int_from_bytes_LE(s):
total = 0
for c in reversed(s):
total = (total << 8) + ord(c)
return total
# Test
data = (
(b'\x01\x02\x03\x04', 0x04030201),
(b'\x01\x02\x03\x04\x05\x06\x07\x08', 0x0807060504030201),
(b'\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89\xab\xcd\xef',
0xefcdab8967452301efcdab8967452301),
)
for s, u in data:
print hexlify(s), u, int_from_bytes_LE(s)
#print(hexlify(s), u, int.from_bytes(s, 'little'))
輸出
01020304 67305985 67305985
0102030405060708 578437695752307201 578437695752307201
abcdefabcdef 318753391026855559389420636404904698625 318753391026855559389420636404904698625
(我把Python 3的打印通話在那裏,所以你可以很容易地驗證我的函數給出了相同的結果int.from_bytes
)。
如果您的數據是真的大和你不想浪費RAM你扭轉字節的字符串,你可以這樣來做:
def int_from_bytes_LE(s):
m = 1
total = 0
for c in s:
total += m * ord(c)
m <<= 8
return total
當然,使用一些RAM爲m
,但它不會像用於反轉輸入字符串的RAM那麼多。
- 1. 如何將數字轉換爲python中的文件的整數
- 2. 如何將整數轉換爲Python中的邏輯數組
- 3. 如何將數組元素轉換爲Python中的整數
- 4. 如何bitarray轉換爲整數的python
- 5. 如何將整數轉換爲python中的ctypes.c_byte
- 6. 如何將字符串中的字節轉換爲整數? Python
- 7. 如何將整數轉換爲python中的單詞?
- 8. 如何將字符串列表轉換爲Python中的整數?
- 9. 如何將Blob轉換爲Int16Array
- 10. 如何將文件Blob轉換爲NSData?
- 11. Python將字符串轉換爲整數?
- 12. Python:將str轉換爲整數
- 13. Python將整數轉換爲日期
- 14. Python,PuLP:將LpVariable轉換爲整數
- 15. 如何將blob數據轉換爲JavaScript中的圖像?
- 16. Blob to String:如何將Blob轉換爲PostgreSQL中StoredProcedure參數的字符串?
- 17. 如何將time.strptime轉換爲datatime.date()的整數Python 2
- 18. 將Blob轉換爲圖像
- 19. 將clob轉換爲blob
- 20. 如何在Python中將正整數列表轉換爲負整數?
- 21. 如何將整數轉換爲LARGE_INTEGER
- 22. 如何將Java.Lang.Enum轉換爲整數?
- 23. 如何將整數[70]轉換爲[.70]?
- 24. 如何將整數流轉換爲java.awt.Point?
- 25. 如何將日期轉換爲整數?
- 26. 如何將整數轉換爲「X」?
- 27. 如何將system.data.dataRow值轉換爲整數
- 28. 將BLOB轉換爲sql中的文本
- 29. MySQL將BLOB內容轉換爲有符號整數
- 30. 無法將cassandra blob /字節字符串轉換爲整數
您可以使用結構模塊。 'N'有多大? –
[Python中整數的字節順序可能重複](http://stackoverflow.com/questions/1400012/endianness-of-integers-in-python) –
'N'可以達到文件大小。 – warchantua