【CSEN009】The transform between byte data and integer/float data

CS model could receive the specifed length byte array data from host pc with socket_read_byte_list command. And users are able to transform these data to specified type( like float, int16 or int32).

For example, the byte array from host PC is [0x3F,0x80,0x00,0x00], users will get the result [4,63,128,0,0] (the first number is data length, if lost some value in the array, it will be replaced with -1)

If users need transform float, int16 or int32 to byte array, they can use the code as below:

import struct
def Bytes2Float(d,BigEndian=False):
    # input parameter 4 bytes data
    # return 32-bit integer data
    # The default is endian,if need big endian, please addBigEndian=True)
    if BigEndian ==True:
        return struct.unpack('<f', struct.pack('4B', *d))[0]
    else:
        return struct.unpack('>f', struct.pack('4B', *d))[0]

def Bytes2Int32(d,BigEndian=False):
    # input parameter 4 bytes data
    # return signed 32-bit integer data
    # The default is endian,if need big endian, please addBigEndian=True)
    if BigEndian ==True:
        return struct.unpack('<i', struct.pack('4B', *d))[0]
    else:
        return struct.unpack('>i', struct.pack('4B', *d))[0]

def Bytes2Int16(d,BigEndian=False):
    # input parameter 2 bytes data
    # return signed 16-bit integer data
    # The default is endian,if need big endian, please addBigEndian=True)
    if BigEndian ==True:
        return struct.unpack('<h', struct.pack('2B', *d))[0]
    else:
        return struct.unpack('>h', struct.pack('2B', *d))[0]


def Float2Bytes(d,BigEndian=False):
    # input 32-bit integer data
    # return parameter 4 bytes data
    # The default is endian,if need big endian, please addBigEndian=True)
    if BigEndian ==True:
        return list(struct.unpack('4B',struct.pack('f',d)))
    else:
        return list(struct.unpack('4B',struct.pack('>f',d)))

def Int322Bytes(d,BigEndian=False):
    # input 32-bit signed integer data
    # return 4 bytes data
    # The default is endian,if need big endian, please addBigEndian=True)
    if BigEndian ==True:
        return list(struct.unpack('4B',struct.pack('<i',d)))
    else:
        return list(struct.unpack('4B',struct.pack('>i',d)))

def Int162Bytes(d,BigEndian=False):
    # input 16-bit signed integer data
    # return 2 bytes data
    # The default is endian,if need big endian, please addBigEndian=True)
    if BigEndian ==True:
        return list(struct.unpack('2B',struct.pack('<h',d)))
    else:
        return list(struct.unpack('2B',struct.pack('>h',d)))

a = Float2Bytes(1.0)
# Endian as default, the output data should be as below
# a = [0x3F,0x80,0x00,0x00]

a = Float2Bytes(1.0,True)
# Big endian, the output data should be as below
# a = [0x00,0x00,0x80,0x3F]

b = Bytes2Float( [0x3F,0x80,0x00,0x00])
# Endian as default, the output data should be as below
# b = 1.0

b = Bytes2Float( [0x3F,0x80,0x00,0x00],True)
# Big endian, the output data should be as below
# b = 4.600602988224807e-41
点击显示全文
赞同0
发表评论
分享

手机扫码分享
0
272
收藏
举报
收起
登录
  • 密码登录
  • 验证码登录
还没有账号,立即注册
还没有账号,立即注册
注册
已有账号,立即登录
选择发帖板块
上传文件
举报
请选择举报理由
举报
举报说明