-2

I want to convert the String Entered convert into BCD.

a = '2015'
''.join(format(int(c), '04b') for c in str(int(a, 16)))

is giving me '1000001000010011'. But I want it to read 0010 0000 0001 0011 as in unpacked BCD format. Can anyone help me with this?

abhi1610
  • 721
  • 13
  • 28

1 Answers1

2

My friend, as you see, clearer the question make, easier for us to help you.

Here how I managed your problem

no = "2015"
bcd = " ".join(["0"*(4 - len(bin(int(number))[2:])) + bin(int(number))[2:] for number in no])

print bcd

# 0010 0000 0001 0101

0010 0000 0001 0011 was your output and it is wrong. So your issue is not just the spaces between 4-digit representation. However, to resolve it:

The issue of spaces was because of the

''.join()

you used. You needed

' '.join()

instead.

Rockybilly
  • 2,938
  • 1
  • 13
  • 38