printing decimal to binary quick help needed
Page 1 of 18 Replies  7989 Views  Last Post: 29 March 2009  10:18 PM
#1
printing decimal to binary quick help needed
Posted 09 February 2009  02:12 AM
for instance if the number is 7 I need my output to be
0000
0001
0010
0011
0100
0101
0110
0111
the zeros need to be included. Right now I'm having it print but later I'll be storing each character in an array or list. I'll figure that bit out later.
Any help is greatly appreciated.
Replies To: printing decimal to binary quick help needed
#2
Re: printing decimal to binary quick help needed
Posted 09 February 2009  10:09 AM
def DecimalToBinary(decimal): if(decimal == 0): return 0; else: print(decimal % 2); DecimalToBinary((int)(decimal / 2));
This function will take a decimal and print out its binary equivalent. Because of the method it uses the binary is reversed, but you can figure that out!
edit: Because for some reason I originally wrote BinaryToDecimal...
This post has been edited by headways_millennia: 09 February 2009  10:30 AM
#3
Re: printing decimal to binary quick help needed
Posted 09 February 2009  10:38 AM
print "the binary value is: %03b" % binaryVariable
It works almost identically to the printf of java
The funniest part of it all is that it doesn't help me solve my problem, and am now doing this in sage as it has built in functions for just such a problem as the one I'm trying to solve. For those who don't know sage is a powerful math program built on the python language. The two are almost interchangable as you can write a file with the .py extension and change the extension to .sage and sage 9 times out of 10 won't bat an eye at running the program. The benefit of sage is that it has functions such as Integer(x).Binary() which autconverts an int to bin. VERY USEFUL as python requires you find and/or write your own method.
Anyhow, thanks for the input, and all the help. I couldn't make it without the DiC community.
T.
#4
Re: printing decimal to binary quick help needed
Posted 09 February 2009  11:58 AM
#DecimalToBinary def DecimalToBinary(decimal, emptyList): if(decimal == 0): return 0; else: emptyList.append(decimal % 2); DecimalToBinary((int)(decimal / 2), emptyList); #Empty List binary = [] #Loop for cnt in range(7): DecimalToBinary(cnt+1, binary) binary.reverse() for num in binary: print(num, end="") binary = [] print('\n')
Output:
1 10 11 100 101 110 111
#5
Re: printing decimal to binary quick help needed
Posted 09 February 2009  12:35 PM
000
001
010
011
100
101
110
111
(this is all dependent on the initial list I"m given. it could go up to 256). long story short I use the original list, with the newly created one to get the disjunctive normal form of the equation that would result in a certain truth table. Not sure if I need the list to be a multi dimension sort or if a simple list will work. As you've been an immense help, any thoughts on how to do either?
by the way, thanks for all the help. much kudos headed your way
T.
P.S. the zeros are a non issue now as I have ways of fixing it in either sage or python. So any help won't need to include code for that.
This post has been edited by troegs: 09 February 2009  12:38 PM
#6
Re: printing decimal to binary quick help needed
Posted 09 February 2009  12:42 PM
#7
Re: printing decimal to binary quick help needed
Posted 09 February 2009  01:07 PM
Quote
Yes, the values are stored in the list called binary before being printed out, after which the binary list is emptied.
If you want to store the values in a list as they are created you can add this little bit to the above for loop:
#CREATE A LIST valueList = [] #ADD VALUES TO LIST IN FOR LOOP for cnt in range(7): DecimalToBinary(cnt+1, binary) binary.reverse() #Add values here valueList.append(binary) for num in binary: print(num, end="") binary = [] print('\n')
Output:
valueList [[1], [1, 0], [1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]] valueList[0] [1] valueList[1] [1, 0] valueList[2] [1, 1] valueList[3] [1, 0, 0]
etc...
This post has been edited by headways_millennia: 09 February 2009  01:09 PM
#8
Re: printing decimal to binary quick help needed
Posted 09 February 2009  01:42 PM
T.
#9
Re: printing decimal to binary quick help needed
Posted 29 March 2009  10:18 PM
troegs, on 9 Feb, 2009  09:38 AM, said:
print "the binary value is: %03b" % binaryVariable
It works almost identically to the printf of java
The funniest part of it all is that it doesn't help me solve my problem, and am now doing this in sage as it has built in functions for just such a problem as the one I'm trying to solve. For those who don't know sage is a powerful math program built on the python language. The two are almost interchangable as you can write a file with the .py extension and change the extension to .sage and sage 9 times out of 10 won't bat an eye at running the program. The benefit of sage is that it has functions such as Integer(x).Binary() which autconverts an int to bin. VERY USEFUL as python requires you find and/or write your own method.
Anyhow, thanks for the input, and all the help. I couldn't make it without the DiC community.
T.
if %b works, you can use list comprehension:
array = ['%05b' %n for n in range(2**5)]
where 5 is the bitlength
in python 3.0 use:
array = ['{0:0{1}b}'.format(i,bitLength) for i in range(2**bitLength)]
where bitLength is the length of bits
This post has been edited by confucius13: 29 March 2009  10:22 PM
