Solved

Binary Bits to Decimal Numbers

Posted on 2010-08-21
10
500 Views
Last Modified: 2012-05-10
i am interested to get a python module/script that does the following :

Input :

Binary Values

10011100011

Output:

Based on the above binary values/bits, convert the bits into dec equivalent

1024 + not_set + not_set + 128 + 64 + 32 + not_set + not_set + not_set + 2 + 1

Thanks a bunch.

0
Comment
Question by:williamwlk
  • 6
  • 3
10 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 33491679
you mean like:


>>>
>>> int( '10011100011', 2 )
1251
>>>>>> hex( 1251 )
'0x4e3'

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 33491680
Oh, you really want that output?

1024 + not_set + not_set + 128 + 64 + 32 + not_set + not_set + not_set + 2 + 1

one moment.
0
 

Author Comment

by:williamwlk
ID: 33491707
:) Yes
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 350 total points
ID: 33491719
Like this?

Output:

C:\>python plus.py
 1024 +  not_set +  not_set +  128 +  64 +  32 +  not_set +  not_set +  not_set +  2 +  1

val = '10011100011';
L = len( val );
plus = '';
for i in range( L ) :
  c = val[ i ];
  if c == '1' :
    print plus, int( ( '1' + ( '0' * ( L - 1 - i ) ) ), 2 ),
  else :
    print plus, 'not_set',
  plus = '+ ';

Open in new window

0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 350 total points
ID: 33495195
Do you like this better?

Output:
 1024 +  not_set +  not_set +  128 +  64 +  32 +  not_set +  not_set +  not_set +  2 +  1
val = '10011100011'
result = [];
pot = 1
while val :
  val, c = val[ :-1 ], val[ -1 ]
  if c == '1' :
    result.append( pot );
  else :
    result.append( 'not_set' )
  pot *= 2

result.reverse();
plus = '';
for c in result :
  print plus, c,
  plus = '+ ';

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Assisted Solution

by:mish33
mish33 earned 150 total points
ID: 33597732
I'd like more structured version:
def p2(n):
  p = 1
  s = []
  while n:
    s.append(n&1 and p or 0)
    p *= 2
    n /= 2
  return s[::-1]

value = '10011100011'
print ' + '.join(str(b or 'not set') for b in p2(int(value, 2)))

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 33597845
the "or 0" appears to be unnecessary...


interesting solution though
def p2(n):
  p = 1
  s = []
  while n:
    s.append(n&1 and p)
    p *= 2
    n /= 2
  return s[::-1]

value = '10011100011'
print ' + '.join(str(b or 'not set') for b in p2(int(value, 2)))

Open in new window

0
 

Author Closing Comment

by:williamwlk
ID: 33639756
Thanks Bros! Cheers.
W
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 33639828
Thanks for the grade & points.

Good luck & have a great day.
0
 

Author Comment

by:williamwlk
ID: 33639865
You star! HonorGod! Thanks for helping me out too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python No Modules Named Defaults 7 137
IronPython and Visual Studio 2013+ 2 92
python - Creating a function to stop repetition of code 2 313
linux crontab output 3 68
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now