i'm trying to create a module to pack and unpack data like: pack: encode them to binary string unpack: decode them from binary string to integr : ord(decodet_binary_string) but i have long time trying to continue my code and the same error was shown to me the cause of encoding to binary is: some data can't be converted to utf-8 or utf-16 or utf-32 so when we turn them to binary string and encode the we can use utf-8 to encode them :)
class tlv_packet:
def __init__(self, bitseq = True):
self.tlv_packet = True
self.bitseq = bitseq
def encode(self, data):
self.data = ""
if data.isascii() == True:
self.type = 0
else:
self.type = 256
for char in range(0, len(data)):
self.data += ""
self.binary_type = bin(self.type)
self.binary_string = "".join(f"{bin(ord(i))}" for i in data) #:08b}" for i in s)
self.binary_length = len(self.binary_string)
return self.binary_type, self.binary_length, self.binary_string
def decode(self, data = ""):
self.data_vector = data.split('b')
self.data = ""
for count in range(0, len(self.data_vector)):
char = self.data_vector[count]
order = 0b0
for char_order in range(0, len(self.data_vector[count])):
if self.data_vector[count][char_order] == '1':
order += 1
elif self.data_vector[count][char_order] == '0':
order += 0
print(self.data_vector[count])
self.data += chr(order)
self.data_length = len(self.data)
return self.data_length, self.data