2024-06-24

2024-06-23  本文已影响0人  zxlele_c763

python string operation

about hex , zfill opearton

#step5: handoverreq
'''
kgnbstarinput = [0b01011101, 0b00011001, 0b11101111, 0b01100100, 0b11010010, 0b11111100, 0b01010110, 0b00010011,
0b00110110, 0b11100000, 0b10010011, 0b01001010, 0b00101110, 0b00010110, 0b11101011, 0b10101010,
0b00100110, 0b10100101, 0b00011110, 0b10101110, 0b00101010, 0b01010110, 0b01011101, 0b10000000,
0b10010011, 0b10110000, 0b01001100, 0b00000001, 0b10010010, 0b00111011, 0b01000001, 0b00000101]
#5d19ef64d2fc561336e0934a2e16ebaa26a51eae2a565d8093b04c1923b415

'''
kgnbstarinput = [0b01011101, 0b00011001, 0b11101111, 0b01100100, 0b00000101]
#5d19ef6405

kgnbstarhex_list = [hex(num)[2:].zfill(2) for num in kgnbstarinput]
print("kgnbstarhex_list is: ", kgnbstarhex_list)

kgnbstarhex_list3 = [hex(num).zfill(2) for num in kgnbstarinput]
print("kgnbstarhex_list3 is: ", kgnbstarhex_list3)

kgnbstarhex_list1 = ['0x' + num  for num in kgnbstarhex_list]
print("kgnbstarhex_list1 is: ", kgnbstarhex_list1)
print("type(kgnbstarhex_list1[0] is: ",type(kgnbstarhex_list1[0]))
kgnbstarhex_list2 = [hex(int(num, 16)) for num in kgnbstarhex_list1]
print("kgnbstarhex_list2 is: ", kgnbstarhex_list2)
print("type(kgnbstarhex_list2[0] is: ",type(kgnbstarhex_list2[0]))

kgnbstarhex_string = "".join(kgnbstarhex_list)
print("kgnbstarhex_string is: ", kgnbstarhex_string)
kgnbstarhex_string1 = ", 0x".join(kgnbstarhex_list)
print("kgnbstarhex_string1 is: ", kgnbstarhex_string1)

This line of Python code is performing a few operations on the variable num. Let's break it down:
1. hex(num): This is converting the number num to a hexadecimal string. The hex() function in Python converts an integer number to a lowercase hexadecimal string prefixed with "0x". For example, hex(255) would return the string '0xff'.
2. [2:]: This is a slicing operation that removes the first two characters from the string. In this case, it's removing the '0x' prefix from the hexadecimal string. For example, if the hexadecimal string is '0xff', hex(255)[2:] would return the string 'ff'.
3. .zfill(2): This is calling the zfill() method on the string. The zfill() method returns a copy of the string left filled with zeros ('0') to a total of specified width. In this case, it's ensuring that the string is at least 2 characters long, padding with zeros if necessary. For example, if the string after the slicing operation is 'f', hex(255)[2:].zfill(2) would return the string '0f'.
So, in summary, this line of code is converting the number num to a hexadecimal string, removing the '0x' prefix, and ensuring that the resulting string is at least 2 characters long, padding with zeros if necessary.

about string string::join operation

In Python, the join method is used to concatenate the elements of an iterable (like a list or tuple) into a single string. The string that join is called on is used as the separator between elements. Here's the general syntax:

Important Points

my_list = ['apple', 'banana', 'cherry']
joined_string = ", ".join(my_list)
print(joined_string)  # Output: apple, banana, cherry

my_tuple = ('Monday', 'Tuesday', 'Wednesday')
joined_string = " - ".join(my_tuple)
print(joined_string)  # Output: Monday - Tuesday - Wednesday

about hex(int(num, 16)

The expression int(num, 16) in Python converts a string num that represents a number in hexadecimal format into its equivalent integer value. The int function can take up to two arguments: the string to convert, and the base of the number system of that string. In this case, 16 specifies that the number is in base 16, or hexadecimal.

Here's a breakdown:

So, int(num, 16) converts the hexadecimal string num into an integer. For example, int('1a', 16) would return 26, because 1a in hexadecimal is equal to 26 in decimal.

kgnbstarhex_list is:  ['5d', '19', 'ef', '64', '05']
kgnbstarhex_list3 is:  ['0x5d', '0x19', '0xef', '0x64', '0x5']
kgnbstarhex_list1 is:  ['0x5d', '0x19', '0xef', '0x64', '0x05']
type(kgnbstarhex_list1[0] is:  <class 'str'>
kgnbstarhex_list2 is:  ['0x5d', '0x19', '0xef', '0x64', '0x5']
type(kgnbstarhex_list2[0] is:  <class 'str'>
kgnbstarhex_string is:  5d19ef6405
kgnbstarhex_string1 is:  5d, 0x19, 0xef, 0x64, 0x05
上一篇下一篇

猜你喜欢

热点阅读