I have a byte array b_a
. len(b_a)
returns 24. But I want to make b_a
's length 16 without data loss. Is there any way of that?
Depends how do you want to achieve that. If its length is 24, then there is 24 elements in it, obviously. If you want to make it 16, you will have to remove 8 elements.
You can do this by:
Slicing:
b_a = b_a[:16]
or
b_a = b_a[8:]
or any combination:
b_a = b_a[2:18]
Or you could .pop()
items:
b_a.pop(). # do this 8 times
or
b_a.pop(some_index). # do this 8 times
You can also part bytearray into slices like strings. So len(mybytearray[0:16])
But this will not work well. Because in different encoding sets (utf-8, ascii...), characters occupy different amounts of number. That's why we need to know what you encoded your bytearray with.
It may still work for you:
mybyte = bytearray("uğurcanveözlemkardeşler", 'utf-8')
mybyte16 = mybyte[0:16]
mybyte16
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.