[英]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.