Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to convert binary literal like 0b01011 into the binary string like 0101101101 . How i should go in here.

here suppose A=81 (hex)

>>> a = bin(int(str('A'),16))
>>> print a 

returns 0b1010 and i want like 10000001 (binary string)

share|improve this question
2  
How are you intending to get 1010110 from the number ten (or the number one hundred twenty-nine)? If you just don't want the 0b at the front, you can chop it off with a[2:]. –  BrenBarn Jan 1 at 19:44
2  
Try format(81, 'b'). –  Ashwini Chaudhary Jan 1 at 19:44
 
i meant i want the conversion from any binary literal to its corresponding string. so dont consider what i wrote in literal and string –  user3098378 Jan 1 at 19:46
1  
So I guess you're looking for this: bin(int(str(A),16))[2:] (note the lack of '') –  freakish Jan 1 at 19:52
1  
@user3098378: Sure, fine, but edit your question so it says what data you have and what output you want. The question as written does not make sense, and doesn't seem to bear any relation to what you're trying to do. –  BrenBarn Jan 1 at 20:02
show 11 more comments

4 Answers

With python's newer string handling:

>>> print '{:08b}'.format(81)
01010001

>>> print '{:08b}'.format(ord('A'))
01000001

The '08' specifies an output size of 8, padded with zeros, so this this, by default, shows a whole byte. Without the zero, it is padded with spaces:

>>>  print '{:8b}'.format(ord('A'))
 1000001

Without the '8', the output string is only as large as needed:

>>> print '{:b}'.format(ord('A'))
1000001
share|improve this answer
 
Or just with '{:b}'.format(81) if you don't want to specify a minimum number of characters to use. –  Chris Jan 1 at 19:50
add comment

I don't really get what you're asking, but you can just do what you're already doing and chop off the 0b:

>>> bin(0b110110)[2:]
'110110'

When you enter a binary literal, it turns into a number. The number doesn't depend on its representation in binary or any other base; it's just stored as an integer value. So it doesn't matter whether you input it as a binary literal or not, you can still use bin to get its binary string representation.

share|improve this answer
 
see, after putting A=81 (A in hex), i get the binary literal 0b1010 though its binary string representation is 10000001 which is what i want –  user3098378 Jan 1 at 19:49
 
@user3098378: What do you mean "A in hex"? How did you put "A in hex"? –  BrenBarn Jan 1 at 19:53
add comment

You need to use the ord function to convert the string 'A' into its corresponding ASCII character. (Applying str to 'A' doesn't achieve anything.) Then you can convert that integer into a binary string, thus:

>>> print ord('A')
65
>>> a = bin(ord('A'))
>>> print a
0b1000001

If you don't want the '0b' prefix:

>>> print a[2:]
1000001
share|improve this answer
1  
This is incorrect, I still don't know why OP accepted it. –  Ashwini Chaudhary Jan 1 at 20:01
 
yeah. m not getting the required output, but same output for different inputs –  user3098378 Jan 1 at 20:16
 
@AshwiniChaudhary How is it incorrect? This answer was written based on the code in original question, and before the comment discussion above. It is still quite unclear precisely what the poster is asking, but presumably if they accepted it, there was something useful in there. –  gavinb Jan 1 at 20:19
 
@user3098378 Please clarify your question. There is too much confusion about what you're actually asking. What inputs do you have, and what output format are you after? –  gavinb Jan 1 at 20:23
 
suppose my output is of 6bit (101001) or 7(1001110) bit and i want to add 0s at the starting to make it an 8 bit output, how to do it? also tell is it possible to do it automatically only after python sees the output is not of 8 bit? –  user3098378 Jan 1 at 20:27
show 2 more comments

As far as I understand (based on comments) you get the number as an integer:

A = 81

and then you want to interpret it as hexadecimal. In order to do that cast it to a string (note that I'm casting a variable A, not a string 'A'):

str(A)

then cast it to int base 16:

int(str(A), 16)

finally convert it to a binary representation:

bin(int(str(A), 16))

and take all characters from second position (ommit first two):

bin(int(str(A), 16))[2:]
share|improve this answer
 
suppose my output is of 6bit (101001) or 7(1001110) bit and i want to add 0s at the starting to make it an 8 bit output, how to do it? also tell is it possible to do it automatically only after python sees the output is not of 8 bit? –  user3098378 Jan 1 at 20:30
 
@user3098378 If you store the end result in variable x then (afterwards) simply do x = "0"*max(SIZE-len(x), 0) + x and define SIZE=6 (or 7 or whatever you want) before that. –  freakish Jan 1 at 20:32
 
see it should be of 8 bit only. if it return 8 then no padding of 0s, if less then that then accordingly left padding of 0s –  user3098378 Jan 1 at 20:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.