hi,

i have a script that converts hex to bin and bin to hex back. however, when converting back to hex the leading 0s are truncated. how to maintain the leading 0s? i'm using Python 2.5 and Win XP. tq

``````import binascii
import string

def byte_to_binary(n):
return ''.join(str((n & (1 << i)) and 1) for i in reversed(range(8)))

def hex_to_binary(h):
return ''.join(byte_to_binary(ord(b)) for b in binascii.unhexlify(h))

def bintohex(s):
return ''.join([ "%x"%string.atoi(bin,2) for bin in s.split() ])

array0 = '000a00000003c0000030000c1f800000'

a=hex_to_binary(array0)
print "Convert array0 to bin:",a
print "Convert array0 back to hex:",bintohex(a)

if bintohex(a)==array0:
print "Match"
else:
print "Not match"``````

#########result######################

>>>
Convert array0 to bin: 00000000000010100000000000000000000000000000001111000000000000000000000000110000000000000000110000011111100000000000000000000000
Convert array0 back to hex: a00000003c0000030000c1f800000
Not match
>>>

## All 5 Replies

I managed to do it like this

``````def bintohex(s):
t = ''.join(chr(int(s[i:i+8],2)) for i in xrange(0, len(s), 8))
return binascii.hexlify(t)``````

All of this would be easier with python >= 2.6 because of the builtin `bin` function. See this code snippet for example http://www.daniweb.com/code/snippet221031.html.

In python 2.6, you could probably write

``````def hex_to_binary(s):
return bin(int("1" + s, 16))[3:]

def bintohex(s):
return "{0:#0x}".format(int("1"+z, 2))[3:]``````

hi,

yes agree with you if only i can upgrade to Python 2.6 as there are some legacy scripts written on Python 2.5. thanks for your advice.
final code is:

``````import binascii
import string

def byte_to_binary(n):
return ''.join(str((n & (1 << i)) and 1) for i in reversed(range(8)))

def hex_to_binary(h):
return ''.join(byte_to_binary(ord(b)) for b in binascii.unhexlify(h))

##def bintohex(s):
##    return ''.join([ "%x"%string.atoi(bin,2) for bin in s.split() ])

def bintohex(s):
t = ''.join(chr(int(s[i:i+8],2)) for i in xrange(0, len(s), 8))
return binascii.hexlify(t)

array0 = '000a00000003c0000030000c1f800000'
print array0

a=hex_to_binary(array0)
print "Convert array0 to bin:",a
print "Convert array0 back to hex:",bintohex(a)

if bintohex(a)==array0:
print "Match"
else:
print "Not match"``````

#####result#######
>>>
000a00000003c0000030000c1f800000
Convert array0 to bin: 00000000000010100000000000000000000000000000001111000000000000000000000000110000000000000000110000011111100000000000000000000000
Convert array0 back to hex: 000a00000003c0000030000c1f800000
Match
>>>

Can you give example of any code running in 2.5 not running in 2.71 or 2.6.6?

am not sure codes that run in 2.5 but can't run in 2.71 or 2.6.6. by right higher version can run lower version codes.

So you can upgrade to one off those depending on modules you or you can install them both and deside later which will be default in your system. I have in my system 2.71, 2.66, 3.1 and 3.2 installed with very little issues. I keep the 2.6.6 for code needing psyco module.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.