Splitting the bit number

A forum for general discussion of the Python programming language.

Splitting the bit number

Postby zahrein » Thu May 16, 2013 6:08 am

HI , After conversion the hexadecimal to binary numbers. I want to separate 4 bits each so that i can read easily. Tried 1-2 ways but seems not efficient.
For example my output is : 10010111
I want my output to have some space for each 4 bits like this: 1001 0111 . Its easier for me to read. Appreciate your help.

Code: Select all
def heximal_value(value):
    scale = 16
    num_of_bits = 8
    mynum=bin(int(value, scale))[2:].zfill(num_of_bits)
    print mynum






dchoice = 'AF15'
heximal_value(dchoice)
zahrein
 
Posts: 15
Joined: Wed Apr 03, 2013 1:45 am

Re: Splitting the bit number

Postby setrofim » Thu May 16, 2013 6:38 am

Code: Select all
def heximal_value(value):
    scale = 16
    mynum = bin(int(value, scale))[2:]
    print ' '.join(parts(mynum, 4))

def parts(sequence, size):
    for i in xrange(0, len(sequence), size):
        yield sequence[i:i+size]


dchoice = 'AF15'
heximal_value(dchoice)
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Discussions

Who is online

Users browsing this forum: Google Feedfetcher and 4 guests