Home > Back-end >  How to convert binary into sign representation?
How to convert binary into sign representation?

Time:05-06

If an array consist of decimal number which represents as blocks('#') or empty(' '). For example.

A = [31,21,29,19,31]
represents
['11111', '10101', '11101', '10011', '11111']

and I want to have this array to be like this
['#####', '# # #', '### #', '# ##', '#####']

CodePudding user response:

You can use a list comprehension with string formatting (here a f-string) and a translation table:

A = [31,21,29,19,31]  

trans = str.maketrans('01', ' #')
# {48: 32, 49: 35}

out = [f'{x:b}'.translate(trans) for x in A]

output:

['#####', '# # #', '### #', '#  ##', '#####']
  • Related