简体   繁体   English

如何处理 python 中的字节数组中的位以使用十六进制转换导出 integer、字符串和浮点值

[英]How to handle the bits in an array of bytes in python to derive integer, string and float values using hex conversion

Read input bytes from a binary file and convert the bytes to the corresponding hex value to derive the final integer, string and float values.从二进制文件中读取输入字节并将字节转换为相应的十六进制值以获得最终的 integer、字符串和浮点值。

Tried reading the input binary file containing the bytes into an array of bytes and then processed the array values to derive the integer, string and float values.尝试将包含字节的输入二进制文件读入字节数组,然后处理数组值以导出 integer、字符串和浮点值。

# Following example will give the sample python syntax to process the bytes to derive the integer, string and float values. 

# How to process the bit level information from a byte array.

# Read the n number of bytes. 
global byte_array
byte_array = bytearray()
# Read the bytes from the file into byte_array

# Read the first two bytes in the two variables.
byte1 = bin(byte_array[0])[2:].rjust(8,'0')
byte2 = bin(byte_array[1])[2:].rjust(8,'0')

# Bit information, how they are positioned in a byte
# byte1
# 8,7,6,5,4,3,2,1 - bits (8 is MSB and 1 is LSB)
# 0,1,2,3,4,5,6,7 - array position
# 1,2,3,4,5,6,7,8 - bit position 

# Reading the bits 6 to 4 from byte1
byte1_bits_6_to_4 = byte1[2:5] # byte1[<array position>:<bit position>]

# integer of the 4 list significant bit of byte 1
byte1_int = int(byte1[4:8] , 2)

# integer of 4 list significant bit of byte1 and all bits of byte 2
byte1_byte2_int = int(byte1[4:8] + byte2 , 2)

# combined bits of first 2 bytes of byte_array
bytenum = 0
byte1_byte2_16_bit = bin(byte_array[bytenum])[2:].rjust(8,'0') + \
                     bin(byte_array[bytenum + 1])[2:].rjust(8,'0')


# Getting the two most significant bits of byte1_byte2_16_bit
# using <array position>:<bit position> syntax as explained above
byte1_byte2_16_bit_2_MSBits = byte1_byte2_16_bit[0:2]  


# Comparing the byte1_byte2_16_bit_2_MSBits content with specific bit values
if byte1_byte2_16_bit_2_MSBits == '01':
    print( f"byte1_byte2_16_bit_2_MSBits = {byte1_byte2_16_bit_2_MSBits}" )

# Calculating integer value of the concatenation of the least 7 significant bits of byte1 and byte2
byte1_byte2_int = int(byte1[1:8] + byte2[1:8] , 2) # check the bits, array position and bit position above


# Concatenating multiple bytes and then extracting the hex value of each nibble
# Each nibble represents a digit in the YYYYMMDD 
i = 0
concat_string = ""
while i < 4
    concat_string = concat_string + bin(byte_array[i])[2:].rjust(8, '0')
    i = i + 1

# Each nibble contains a number 
# e.g. 20230102 --> 2023-01-02 
Date_hex_value = hex(int(concat_string, 2))[2:]
Date_value = Date_hex_value[0:4] + '-' + Date_hex_value[4:6] + '-' + Date_hex_value[6:]


# Getting character string from input byte array
string_bin = ""
i = 0
while i < 5
    string_bin = string_bin + bin(byte_array[i])[2:].rjust(8,'0')
    i = i + 1

string_value = "".join([chr(int(string_bin[i:i+8],2)) for i in range(0, len(string_bin), 8)])


# Extracting the float value
# The first byte contains the exponent value. 
# The rest of the bytes contain the integer value which should be divided as per the exponent to convert to float value
price = ""
while i < 5
    price = price + bin(byte_array[i])[2:].rjust(8, '0')
    i = i + 1

price_int = int(price[8:], 2)

exponent_value = -128 + int(price[1:8] , 2)
div_num = 1
while exponent_value < 0:
    div_num = div_num * 10
    exponent_value = exponent_value + 1

final_price = price_int / n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用zmq / zeromq从传递给python的字符串表示形式(字节数组)中获取整数/浮点数? - How to get integer / float number from a string representation ( array of bytes ) delivered to python using zmq / zeromq? Python 2字节二进制数组转换为整数或浮点数 - Python 2 bytes binary array conversion to interger or float 在Python中,如何将位数组转换为字节数组? - In Python, how to convert array of bits to array of bytes? 如何在一个字节中设置位对象以表示Python中的十六进制符号 - How to set bits within a bytes Object that represents hex symbols in Python 有没有办法在不进行 ascii 转换且不使用循环的情况下将 python3 字节打印为十六进制字符串? - Is there way to print python3 bytes as hex string without ascii conversion and without using a loop? Python 二进制浮点数到 integer 转换使用 ctypes - Python binary float to integer conversion using ctypes Numpy Int Array 到 HEX 然后到字符串转换 PYTHON - Numpy Int Array to HEX and then to String Conversion PYTHON python,十六进制值转换为字符串/整数 - python, hex values to convert to a string/integer Python字符串到十六进制字节 - Python String to Hex Bytes Python:如何将包含十六进制字节的字符串转换为十六进制字符串 - Python: How to convert a string containing hex bytes to a hex string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM