Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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

2. ## 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.

```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

3. ## The Following User Says Thank You to Freaky Chris For This Useful Post:

chronoz13 (September 16th, 2009)

4. ## 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....

5. ## Re: binary conversion..

```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

Regards,
Chris

6. ## The Following User Says Thank You to Freaky Chris For This Useful Post:

chronoz13 (September 16th, 2009)

7. ## 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.

8. ## 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

9. ## The Following User Says Thank You to Freaky Chris For This Useful Post:

chronoz13 (September 16th, 2009)

10. ## Re: binary conversion..

hehehe.. tnx for the info chris!! hahah

11. ## Re: binary conversion..

ah last chris.. the program that you gave was a hexadecimal conversion right? hehe

12. ## 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