converting binary to decimal
Page 1 of 110 Replies  5699 Views  Last Post: 19 August 2008  02:18 AM
#1
converting binary to decimal
Posted 06 August 2008  11:01 PM
Replies To: converting binary to decimal
#2
Re: converting binary to decimal
Posted 06 August 2008  11:29 PM
#3
Re: converting binary to decimal
Posted 07 August 2008  07:45 AM
String binaryString = "1010"; System.out.println(Integer.parseInt(binaryString, 2));
Post "convert decimal to binary"
int i = 10; String binaryString = Integer.toBinaryString(i); System.out.println(binaryString);
This code is not what you want but I guess but I guess it would help
This post has been edited by lordms12: 07 August 2008  07:47 AM
#4
Re: converting binary to decimal
Posted 07 August 2008  01:13 PM
Anyway:
Each bit in a binary number is a power of 2, starting with 2^0. So the nTH bit represents the value of 2^n, where n>=0 and we count n from right to left.
Therefore:
0101 = 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 0 + 4 + 0 + 1 = 5
Now surely you can turn this into an algorithm?
#5
Re: converting binary to decimal
Posted 13 August 2008  07:57 AM
This post has been edited by eradcoil: 13 August 2008  08:02 AM
#6
Re: converting binary to decimal
Posted 13 August 2008  11:15 AM
If so, then I hope this will prove helpful...its cool either way:
in order to convert a decimal number into a binary one, simply divide by two repeatedly, take the remainders, and write them in reverse order.
10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2/2 = 1 remainder = 0
1/2 = 0 remainder = 1
when you reach zero, you are done, and the answer is the remainders from bottom up : 1010
#7
Re: converting binary to decimal
Posted 18 August 2008  05:45 AM
dobbersp, on 13 Aug, 2008  11:15 AM, said:
If so, then I hope this will prove helpful...its cool either way:
in order to convert a decimal number into a binary one, simply divide by two repeatedly, take the remainders, and write them in reverse order.
10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2/2 = 1 remainder = 0
1/2 = 0 remainder = 1
when you reach zero, you are done, and the answer is the remainders from bottom up : 1010
42 / 2 = 21, remainder = 0
21 / 2 = 10, remainder = 1
10 / 2 = 5, remainder = 0
5 / 2 = 2, remainder = 1
2/2 = 1, remainder = 0
1/2 = 0, remainder = 1
42 = 101010
Well what do you know, it works :)
Let's see if I can do the same for octal:
42 / 8 = 5, remainder = 2
5 / 8 = 0, remainder = 5
42 = 52
And for hexadecimal:
42 / 16 = 2, remainder = 10 = A
2 / 16 = 0, remainder = 2
42 = 2A

However, the original post was about converting binary to decimal, not the other way around (still good to know though).
I also wrote a tutorial about it:
http://www.dreaminco...wtopic59949.htm
This post has been edited by JeroenFM: 18 August 2008  05:48 AM
#8
Re: converting binary to decimal
Posted 18 August 2008  07:56 AM
patjoy_24, on 6 Aug, 2008  11:01 PM, said:
Do you mean mathematically as in the manual process of pen and paper?
or for java iprogramming itself
sites
www.javatutorials.com
www. javacoffebreak.com something like that just search thru good sites that give programing tips
#9
Re: converting binary to decimal
Posted 18 August 2008  08:03 AM
patjoy_24, on 6 Aug, 2008  11:01 PM, said:
Oh not bad
manually guys here have given you a great way of solving it
just divide by base 2 for DecBin
for BinDec ^2 i.e 0011 > 1*2^0 + 1*2^1 + 0*2^2 etc
or the alternative is use the conversion table which is the best short cut once mastered
#10
Re: converting binary to decimal
Posted 18 August 2008  08:08 AM
dobbersp, on 13 Aug, 2008  11:15 AM, said:
If so, then I hope this will prove helpful...its cool either way:
in order to convert a decimal number into a binary one, simply divide by two repeatedly, take the remainders, and write them in reverse order.
10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2/2 = 1 remainder = 0
1/2 = 0 remainder = 1
when you reach zero, you are done, and the answer is the remainders from bottom up : 1010
just to simplify you forgot to add remainders >=5 = 1
<=5 = 0
good stuff
#11
Re: converting binary to decimal
Posted 19 August 2008  02:18 AM
AkEmm, on 18 Aug, 2008  08:08 AM, said:
<=5 = 0
good stuff
Clearly you didn't understand his method, the remainder of any positive number divided by 2 is always either 0 or 1, so it's always less than 5.
Try this:
public int remainderOfDiv2(int number) { return number % 2; }
Pass any positive number, if you find one that yields something other than 1 or 0 let me know.
This post has been edited by JeroenFM: 19 August 2008  02:22 AM
