Thursday 6 June 2019

decimal to binary and alternatively merging binary numbers and then converting merged binary back to decimal python3

st=input();
l1=list(map(int,st))

print(l1)
ans=[]
for x in l1:
    result=''
    c=0
    while x != 0:
        r = x % 2  # gives the exact remainder
        x = x // 2
        result = str(r) + result
        c=c+1
    while(c<4):
        result='0'+result
        c=c+1
    ans.append(result)
f=''
for j in range(4):
    for i in range(len(ans)):
        f=f+ans[i][j]
print(int(f,2))


Input:
23

output:
[2, 3]
['0010', '0011']
00001101
13

No comments:

Post a Comment