python 将小端字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1698036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:53:05  来源:igfitidea点击:

Convert little endian string to integer

pythonnumpywav

提问by Jeffrey Aylesworth

I have read samples out of a wave file using the wave module, but it gives the samples as a string, it's out of wave so it's little endian (for example, \x00).

我已经使用 wave 模块从 wave 文件中读取了样本,但是它将样本作为字符串提供,它不在 wave 中,因此它是小端(例如,\x00)。

What is the easiest way to convert this into a python integer, or a numpy.int16 type? (It will eventually become a numpy.int16, so going directly there is fine).

将其转换为 python 整数或 numpy.int16 类型的最简单方法是什么?(它最终会变成一个 numpy.int16,所以直接去那里很好)。

Code needs to work on little endian and big endian processors.

代码需要在小端和大端处理器上工作。

回答by Ned Batchelder

The structmodule converts packed data to Python values, and vice-versa.

struct模块将打包数据转换为 Python 值,反之亦然。

>>> import struct
>>> struct.unpack("<h", "\x00\x05")
(1280,)
>>> struct.unpack("<h", "\x00\x06")
(1536,)
>>> struct.unpack("<h", "\x01\x06")
(1537,)

"h" means a short int, or 16-bit int. "<" means use little-endian.

“h”表示短整数或 16 位整数。“<”表示使用小端。

回答by Alex Martelli

structis fine if you have to convert one or a small number of 2-byte strings to integers, but arrayand numpyitself are better options. Specifically, numpy.fromstring(called with the appropriate dtypeargument) can directly convert the bytes from your string to an array of (whatever that dtypeis). (If numpy.little_endianis false, you'll then have to swap the bytes -- see herefor more discussion, but basically you'll want to call the byteswapmethod on the array object you just built with fromstring).

struct如果您必须将一个或少量的 2 字节字符串转换为整数,则很好,但arraynumpy本身是更好的选择。具体来说,numpy.fromstring(使用适当的dtype参数调用)可以直接将字符串中的字节转换为数组(无论dtype是什么)。(如果numpy.little_endian为 false,则您将不得不交换字节 - 请参阅此处了解更多讨论,但基本上您将希望byteswap在刚刚构建的数组对象上调用该方法fromstring)。

回答by David M. Helmuth

Kevin Burke's answerto this question works great when your binary string represents a single short integer, but if your string holds binary data representing multiple integers, you will need to add an additional 'h' for each additional integer that the string represents.

当您的二进制字符串表示单个短整数时,Kevin Burke对这个问题的回答非常有效,但如果您的字符串包含表示多个整数的二进制数据,则您需要为字符串表示的每个额外整数添加一个额外的“h”。

For Python 2

对于 Python 2

Convert Little Endian String that represents 2 integers

转换表示2 个整数的Little Endian 字符串

import struct
iValues = struct.unpack("<hh", "\x00\x04\x01\x05")
print(iValues)

Output: (1024, 1281)

输出:(1024, 1281)

Convert Little Endian String that represents 3 integers

转换表示3 个整数的Little Endian 字符串

import struct
iValues = struct.unpack("<hhh", "\x00\x04\x01\x05\x03\x04")
print(iValues)

Output: (1024, 1281, 1027)

输出:(1024、1281、1027)

Obviously, it's not realistic to always guess how many "h" characters are needed, so:

显然,总是猜测需要多少个“h”字符是不现实的,所以:

import struct

# A string that holds some unknown quantity of integers in binary form
strBinary_Values = "\x00\x04\x01\x05\x03\x04"

# Calculate the number of integers that are represented by binary string data
iQty_of_Values = len(strBinary_Values)/2

# Produce the string of required "h" values
h = "h" * int(iQty_of_Values)

iValues = struct.unpack("<"+h, strBinary_Values)
print(iValues)

Output: (1024, 1281, 1027)

输出:(1024、1281、1027)

For Python 3

对于 Python 3

import struct

# A string that holds some unknown quantity of integers in binary form
strBinary_Values = "\x00\x04\x01\x05\x03\x04"

# Calculate the number of integers that are represented by binary string data
iQty_of_Values = len(strBinary_Values)/2

# Produce the string of required "h" values
h = "h" * int(iQty_of_Values)

iValues = struct.unpack("<"+h, bytes(strBinary_Values, "utf8"))
print(iValues)

Output: (1024, 1281, 1027)

输出:(1024、1281、1027)