In [46]:
# comments : code for ascii to binary
import binascii




In [48]:
# convert ascii to hex
a = binascii.hexlify("1")
b = binascii.hexlify("0")
print a, b


 31 30

In [49]:
# convert hex to binary
c = binascii.unhexlify("31")
d = binascii.unhexlify(b)
e = binascii.unhexlify(a)
print d, e


0 1

In [50]:
# comment ascii to hex
f = binascii.b2a_hex("1")
print f


31

In [51]:
# comment ascii to hex
f = binascii.b2a_uu("1")
print f


!,0  


In [52]:
bin(int(binascii.hexlify('1'), 16))


Out[52]:
'0b110001'

In [53]:
binascii.hexlify('1')


Out[53]:
'31'

In [54]:
# check base conversion
int('10',16)


Out[54]:
16

In [55]:
int('0', 2)


Out[55]:
0

In [94]:
# convert binary to hex value
'%X' % int('1111', 2)


Out[94]:
'F'

In [95]:
# convert binary to hex value
'%X' % int('110001', 2)


Out[95]:
'31'

In [97]:
# convert binary to ascii value
binascii.unhexlify('%X' % int('110001', 2))


Out[97]:
'1'

In [92]:
# convert binary to int value
int('1111', 2)


Out[92]:
15

In [78]:
int('31', 16)


Out[78]:
49

In [89]:
bin(7)


Out[89]:
'0b111'

In [77]:
binascii.unhexlify('31')


Out[77]:
'1'

In [159]:
# convert ascii zero and one values into binary encoding, error during all zero value and some other values also
a = "11111111"
# convert binary to ascii value
binascii.unhexlify('%X' % int(a, 2))


Out[159]:
'\xff'

In [153]:
# convert ascii zero and one values into binary encoding
a = "01111110"
# convert binary to ascii value
b = binascii.unhexlify('%X' % int(a, 2))

In [154]:
f = open("a2b-out.fp", "w")
print f, b


<open file 'a2b-out.fp', mode 'w' at 0x105c321e0> ~

In [155]:
f.write(b)
f.close()

In [ ]:


In [ ]: