Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中buf.readUInt16LE(offset)的等价物是什么_Python_Node.js - Fatal编程技术网

Python中buf.readUInt16LE(offset)的等价物是什么

Python中buf.readUInt16LE(offset)的等价物是什么,python,node.js,Python,Node.js,我正在寻找Python中这个函数的等价物(来自node.js) 偏移量:开始读取之前要跳过的字节数。必须满足0我知道没有这样的函数,尽管可能有一个包可以做到这一点。 使用标准库实用程序,可以使用struct.unpack和io.BytesIO转换和处理(二进制)缓冲区 下面是一个示例(Python 3)。如果感觉线条有点紧凑,道歉;我希望它仍然是直截了当的阅读 import io import struct buf = io.BytesIO(bytearray([0x12, 0x34, 0x

我正在寻找Python中这个函数的等价物(来自node.js)


偏移量:开始读取之前要跳过的字节数。必须满足0我知道没有这样的函数,尽管可能有一个包可以做到这一点。 使用标准库实用程序,可以使用
struct.unpack
io.BytesIO
转换和处理(二进制)缓冲区

下面是一个示例(Python 3)。如果感觉线条有点紧凑,道歉;我希望它仍然是直截了当的阅读

import io
import struct

buf = io.BytesIO(bytearray([0x12, 0x34, 0x56]))

# < and > for little and big endian, H for 2-byte unsigned short.
assert format(struct.unpack(">H", buf.read(2))[0], '02x') == "1234"

# buf.seek to reset to byte 0 (1, 2) of the stream; also for file objects
buf.seek(0)
assert format(struct.unpack("<H", buf.read(2))[0], '02x') == "3412"

buf.seek(1)
assert format(struct.unpack(">H", buf.read(2))[0], '02x') == "3456"

buf.seek(1)
assert format(struct.unpack("<H", buf.read(2))[0], '02x') == "5634"

buf.seek(2)
try:
    format(struct.unpack("<H", buf.read(2))[0], '02x')
except struct.error as exc:
    assert exc.args[0] == "unpack requires a buffer of 2 bytes"
else:
    assert False, "exception failed to be raised"
导入io
导入结构
buf=io.BytesIO(bytearray([0x12,0x34,0x56]))
#<和>表示小端和大端,H表示2字节无符号短端。
断言格式(struct.unpack(“>H”,buf.read(2))[0],'02x')==“1234”
#buf.寻求重置为流的字节0(1,2);也适用于文件对象
buf.seek(0)
断言格式(struct.unpack(“H”,buf.read(2))[0],'02x')==“3456”
buf.seek(1)

assert format(struct.unpack)(“我知道没有这样的函数,尽管可能有一个包可以这样做。 使用标准库实用程序,可以使用
struct.unpack
io.BytesIO
转换和处理(二进制)缓冲区

下面是一个例子(Python3)。如果行感觉有点紧凑,我表示歉意;我希望它仍然很容易阅读

import io
import struct

buf = io.BytesIO(bytearray([0x12, 0x34, 0x56]))

# < and > for little and big endian, H for 2-byte unsigned short.
assert format(struct.unpack(">H", buf.read(2))[0], '02x') == "1234"

# buf.seek to reset to byte 0 (1, 2) of the stream; also for file objects
buf.seek(0)
assert format(struct.unpack("<H", buf.read(2))[0], '02x') == "3412"

buf.seek(1)
assert format(struct.unpack(">H", buf.read(2))[0], '02x') == "3456"

buf.seek(1)
assert format(struct.unpack("<H", buf.read(2))[0], '02x') == "5634"

buf.seek(2)
try:
    format(struct.unpack("<H", buf.read(2))[0], '02x')
except struct.error as exc:
    assert exc.args[0] == "unpack requires a buffer of 2 bytes"
else:
    assert False, "exception failed to be raised"
导入io
导入结构
buf=io.BytesIO(bytearray([0x12,0x34,0x56]))
#<和>表示小端和大端,H表示2字节无符号短端。
断言格式(struct.unpack(“>H”,buf.read(2))[0],'02x')==“1234”
#buf.seek重置为流的字节0(1,2);也适用于文件对象
buf.seek(0)
断言格式(struct.unpack(“H”,buf.read(2))[0],'02x')==“3456”
buf.seek(1)
断言格式(结构解包(“