is it possible to convert a string to an ascii binary representation for example
string = "h"
h in binary = 01001000
string = "h"
h in binary = 01001000
def dec2bin(num):
''' Convert a decimal integer to base 2.'''
if num == 0:
return ''
num, rem = divmod(num, 2)
return dec2bin(num)+str(rem)
>>> dec2bin(ord('h'))
'1101000'
>>>
>>> num, rem = divmod(100, 2) >>> num 50 >>> rem 0 >>> num, rem = divmod(1, 2) >>> num 0 >>> rem 1 >>> 100%2 0 >>> 1%2 1 >>> divmod(101, 2) (50, 1) >>>
Comment