Page 1 of 1

convert in HEX

Posted: Thu Jun 18, 2020 7:53 am
by bellad
hello ,
i try convert

Code: Select all

 mem=[1,0,0,1,0,1,0,0,0,0,0,0]
in Hexadecimal
i try with :

Code: Select all

res = ''.join(format(x, '02x') for x in mem)
but :

Code: Select all

NameError: name 'format' isn't defined
thank

Re: convert in HEX

Posted: Thu Jun 18, 2020 8:09 am
by Roberthh
try:

Code: Select all

res = ''.join("%02x" % x for x in mem)
or

Code: Select all

res = ''.join('{:02x}'.format(x) for x in mem)

Re: convert in HEX

Posted: Thu Jun 18, 2020 8:22 am
by bellad
ok thank Robert

for the both =

Code: Select all

'010000010001000000000000'

Re: convert in HEX

Posted: Sat Jun 20, 2020 12:25 am
by jcw
Did you mean binary to hex?

Code: Select all

>>> mem=[1,0,0,1,0,1,0,0,0,0,0,0]
>>> ''.join(map(str, mem))
'100101000000'
>>> int(''.join(map(str, mem)),2)
2368
>>> '%x' % int(''.join(map(str, mem)),2)
'940'