Python : 如何将 Bytes like object 转换为 uint16 的数组?

Python : How to convert Bytes like object to Array of uint16?

提问人:MarcMVT 提问时间:12/5/2022 最后编辑:MarcMVT 更新时间:12/7/2022 访问量:478

问:

我有一个来自 udp 流的类似字节的对象。

在 Matlab 中,我可以通过以下代码将字节转换为 uint16 的列向量:

% Build UDP Connection and collect binary data
udpr = dsp.UDPReceiver('LocalIPPort',5005,'ReceiveBufferSize',3200,'MessageDataType','uint16','MaximumMessageLength',640)
setup(udpr)
data = udpr();

为了将列向量转换回具有已知宽度(16 列)的数组,我使用以下代码:

rows_result = length(data)/16;
result = zeros(rows_result,16);
pointer=0;
for i=1:rows_result
    for j=1:16
        result(i,j)=data(pointer+j);
    end
    pointer=pointer+16;
end

这样我就得到了一个 N x 16 的 uint16 值数组。

如何在 Python 中执行类似操作?

我使用以下代码:

import socket
import struct

UDP_IP = "127.0.0.1" 
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes

    value_type = type(data)
    value = list(data)
    print(value_type)
    print(value)

函数 list() 似乎只读取第一个字节,而不是 uint16 值所需的前两个字节。

我尝试了struct.unpack('H')但没有结果,但我找不到一种方法来转换我的变量“数据”。

我也尝试过int.from_bytes,但我不知道如何告诉函数每 2 个字节转换一次...... 我失败了数组模块和array.frombytes()

我将不胜感激的帮助......

此致敬意 马克

编辑 07.12.22: 下面是一些示例数据。我用numpy创建它。 这将是发送方脚本 (sender.py)

import socket
import time
import struct
# generate random integer values
import numpy as np
# seed random number generator
np.random.seed(1)


UDP_IP = "127.0.0.1"
UDP_PORT = 5005

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)


sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
#sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

while True:
    # generate random array of 20x16 values
    input_matrix = np.random.randint(0, 4095, [20,16],dtype=np.uint16)

    print(input_matrix)

    
    MESSAGE = input_matrix
    sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
    time.sleep(3)

出于测试目的,测试数组大小始终相同。稍后,行数可以更改。

这里是 reciever.py 的脚本

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))


while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print(data)
python matlab udp 字节 uint16

评论

0赞 Axe319 12/5/2022
你能给出转换后的样本和预期结果吗?data
0赞 Community 12/5/2022
请澄清您的具体问题或提供其他详细信息以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。
0赞 Mark Tolonen 12/6/2022
print(data)因此,我们了解您收到了什么
0赞 MarcMVT 12/7/2022
请看一下今天的编辑。我希望你能以这种方式重新创建我的字节类对象。尚未定义 UDP 数据包的最大大小。但我认为 1024 Byte 可能是现实的。

答:

0赞 Mark Tolonen 12/6/2022 #1

假设是表示 16 位数据的偶数字节:data

>>> import array
>>> data = bytes([1,0,2,0,3,0,4,0,5,0])
>>> data
b'\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00'
>>> a = array.array('H',data)
>>> a
array('H', [1, 2, 3, 4, 5])
>>> a[0]
1
>>> a[4]
5
>>> list(a)
[1, 2, 3, 4, 5]

如果数据是 big-endian,请切换字节顺序:

>>> a
array('H', [1, 2, 3, 4, 5])
>>> a.byteswap()
>>> a
array('H', [256, 512, 768, 1024, 1280])