how to convert bytes to binary using python
Question:
so i want convert bytes to binary in python, but when i run it, there’s none in the result and i got error:
‘NoneType’ object is not iterable
here’s the code i tried
//func biner
def biner(password):
print(password)
password[0]
for my_byte in password:
print(f'{my_byte:0>8b}', end=' ')
//func to use the result from func biner
def skalar(key, biner):
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
jumbin2 = len(key)
print(jumbin2)
jumbin = biner
print(biner)
hasilenkrip = ''
for a in jumbin:
hasilenkrip += a * jumbin2
print(hasilenkrip)
//how i called the func
enc = b'lx87x87xe6'
ky = maru
tes = biner(enc)
tes1 = skalar(ky, tes)
print(tes1)
Answers:
Your function currently returns None
because there’s no return
statement. Perhaps instead of using print
, you should modify your functions to return an array of outputs.
I suspect what you have in mind is something like this.
# //func biner
def biner(password):
print(password)
password[0]
return ' '.join(f'{my_byte:0>8b}' for my_byte in password)
# //func to use the result from func biner
def skalar(key, biner):
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
jumbin2 = len(key)
print(jumbin2)
jumbin = biner
print(biner)
hasilenkrip = ''
for a in jumbin:
hasilenkrip += a * jumbin2
return hasilenkrip
# print(hasilenkrip)
# //how i called the func
enc = b'lx87x87xe6'
ky = maru
tes = biner(enc)
tes1 = skalar(ky, tes)
print(tes1)
some_bytes = int(input('Enter bytes: '))
print(f'{some_bytes>>4:04b}{some_bytes&0xf:04b} ({some_bytes})')
so i want convert bytes to binary in python, but when i run it, there’s none in the result and i got error:
‘NoneType’ object is not iterable
here’s the code i tried
//func biner
def biner(password):
print(password)
password[0]
for my_byte in password:
print(f'{my_byte:0>8b}', end=' ')
//func to use the result from func biner
def skalar(key, biner):
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
jumbin2 = len(key)
print(jumbin2)
jumbin = biner
print(biner)
hasilenkrip = ''
for a in jumbin:
hasilenkrip += a * jumbin2
print(hasilenkrip)
//how i called the func
enc = b'lx87x87xe6'
ky = maru
tes = biner(enc)
tes1 = skalar(ky, tes)
print(tes1)
Your function currently returns None
because there’s no return
statement. Perhaps instead of using print
, you should modify your functions to return an array of outputs.
I suspect what you have in mind is something like this.
# //func biner
def biner(password):
print(password)
password[0]
return ' '.join(f'{my_byte:0>8b}' for my_byte in password)
# //func to use the result from func biner
def skalar(key, biner):
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
jumbin2 = len(key)
print(jumbin2)
jumbin = biner
print(biner)
hasilenkrip = ''
for a in jumbin:
hasilenkrip += a * jumbin2
return hasilenkrip
# print(hasilenkrip)
# //how i called the func
enc = b'lx87x87xe6'
ky = maru
tes = biner(enc)
tes1 = skalar(ky, tes)
print(tes1)
some_bytes = int(input('Enter bytes: '))
print(f'{some_bytes>>4:04b}{some_bytes&0xf:04b} ({some_bytes})')