79759676

Date: 2025-09-09 08:43:39
Score: 0.5
Natty:
Report link
def convert(val):
     # val is an integer 16 bit   
     valb = '{:16b}'.format(val).strip()
     return ' '.join([valb[i-5:i-1] for i in range(0,-len(valb),-4)][::-1])

Outputs

convert(int('F0F0',16))
Out[1]: '111 1000 0111 1000'
convert(int('001011001010',2))
Out[2]: '1 0110 0101'

If you do want to keep zero you can replace it by :

def convert(val):
     # val is an integer 16 bit   
     valb = '{:16b}'.format(val).replace(' ','0')
     return ' '.join([valb[i-5:i-1] for i in range(0,-len(valb),-4)][::-1])

Output will be:

convert(int('001011001010',2))
Out[3]: '000 0001 0110 0101'
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: kyro