# binary conversion..

• September 16th, 2009, 06:23 AM
chronoz13
binary conversion..
anyone can make me a simple program that converts decimal to binary and to decimal again..

i just want to know how to make its structure in program...
its not that easy. althoug i know the process of it and i cant do it it PAPER.

and i will study its algorithm and then apply it in converting in hex and octal..

im getting some hard time with this
• September 16th, 2009, 06:27 AM
Freaky Chris
Re: binary conversion..
Some time ago I wrote this in C++. You shouldn't have a problem reading the Syntax, if you do let me know.

Code :

```std::string conv(std::string number, int startBase, int endBase){ if(startBase > 16 || endBase > 16) return "BASE ERROR"; char NUMS[] = "0123456789ABCDEF"; std::string result = ""; int temp = 0, x; bool found = false; for(int i = 0; i < number.length(); i++){ for(x = 0; x < startBase; x++){ if(NUMS[x] == number[number.length()-(i+1)]){ found = true; break; } } if(!found) return "NUMBER ERROR"; temp += (x*power(startBase, i)); } do{ result.push_back(NUMS[temp%endBase]); temp /= endBase; }while(temp != 0); return std::string(result.rbegin(), result.rend()); }   long long power(int num, int pow){ if(pow == 0) return 1; return num*power(num, pow-1); }```

Enjoy the studying of it :)

Regards,
Chris
• September 16th, 2009, 09:20 AM
chronoz13
Re: binary conversion..
im not really familiar with C .. can you please convert it into java... ?? ill study both of it ,in C and in Java

sori for that....
• September 16th, 2009, 09:34 AM
Freaky Chris
Re: binary conversion..
Code :

```public String conv(String number, int startBase, int endBase){ if(startBase > 16 || endBase > 16) return "BASE ERROR"; char NUMS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; String result = ""; int temp = 0; int x; boolean found = false; for(int i = 0; i < number.length(); i++){ for(x = 0; x < startBase; x++){ if(NUMS[x] == number.charAt(number.length()-(i+1))){ found = true; break; } } if(!found) return "NUMBER ERROR"; temp += (x*power(startBase, i)); } do{ result += NUMS[temp%endBase]; temp /= endBase; }while(temp != 0); return new StringBuilder(result).reverse(); }   public long power(int num, int pow){ if(pow == 0) return 1; return num*power(num, pow-1); }```

I think i've made all the conversions...there are not many lol! I'm amazed you needed me to do it :P

Regards,
Chris
• September 16th, 2009, 09:42 AM
chronoz13
Re: binary conversion..
sori to bother you.. hehehe anyway tnx for that,!!..

ah one more thing chris..

what does java use? ASCII or UNICODE?

once i input some numbers or characters it returns something like ASCII or UNICODE.
im curious of what kind of character code does java use.
• September 16th, 2009, 09:47 AM
Freaky Chris
Re: binary conversion..
It uses ASCII, read() returns an integer which represents the ASCII value for the character it has read. If you enter 'A' it will return 65 :).

At least as far as I'm aware, I don't have a compiler infront of me to test hehe!

Regards,
Chris
• September 16th, 2009, 09:53 AM
chronoz13
Re: binary conversion..
hehehe.. tnx for the info chris!! hahah \m/
• September 16th, 2009, 10:00 AM
chronoz13
Re: binary conversion..
ah last chris.. the program that you gave was a hexadecimal conversion right? hehe
• September 16th, 2009, 10:47 AM
Freaky Chris
Re: binary conversion..
No, it will go from any base to any base, asuming it is a base below 16 and given in a String format.

Chris