# Thread: I'm trying to convert binary to decimal & hexadecimal. Almost there??

1. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## I'm trying to convert binary to decimal & hexadecimal. Almost there??

You can see where I'm going with this... The Binary to Decimal part is fine, but...the rest is not. When I move this part, it doesn't recognize "str"
Java Code:
```int i= Integer.parseInt(str,2);
System.out.println("Decimal:="+ i);```
Here is the full code:
Java Code:
```import java.io.*;
import java.util.*;
public class BinToDecAndHex{
public static void main(String[] args) throws IOException{
System.out.print("Enter the Binary value: ");
long num = Long.parseLong(str);
long rem;
while(num > 0){
rem = num % 10;
num = num / 10;
if(rem != 0 && rem != 1)
{
System.out.println("This is not a binary number.");
System.exit(0);
}
}
int i= Integer.parseInt(str,2);
System.out.println("Decimal:="+ i);
}
//BINARY TO HEX
public String BinToHex(String numS){
int a;
int ctr=0;
double prod=0;
int number;
int r;
String hex="\0";
for(int j=numS.length();j>0;j--){
a=Character.getNumericValue(numS.charAt(j-1));
prod=prod+(a*Math.pow(2,ctr));
ctr++;
}
number=(int)prod;
do{
r=number%16;
number=number/16;
if(r==10)
hex="A"+hex;
else if(r==11)
hex="B"+hex;
else if(r==12)
hex="C"+hex;
else if(r==13)
hex="D"+hex;
else if(r==14)
hex="E"+hex;
else if(r==15)
hex="F"+hex;
else
hex=r+hex;
}while(number!=0);
return hex;
}
BinToDecAndHex bth=new BinToDecAndHex();
Scanner sc=new Scanner(System.in);
String numS; {
numS=sc.next();
System.out.println(bth.BinToHex(numS));
}
}```

2. ## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

Then you must have moved it into the wrong scope.

Why have you written the BinToHex method if you do not use it?
How is the user supposed to try again if you terminate the program?

3. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

I wrote it so that I could use it, lol. I don't know HOW to. Omg. Lol.
Have mercy... I just mixed them together. I don't know. I just want to know how to make it WORK, not be questioned about why I copied & pasted something...

4. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

You need to sort out your indenting as that's all over the place and makes it hard to see where everything actually sits.

Also, if you have an error you really need to post the full error and highlight where in your code it's happening.

5. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

There is no ERROR. It COMPILES, but the Binary to Hex part just doesn't work. The Binary to Decimal part works fine though. I don't know HOW to make it work. That's why I'm here, because I've been TRYING to get it to work for the past few days, and I have other assignments to do.

6. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

And what does "doesn't work" mean?

The above code is (as I said) poorly indented which makes following the flow very difficult...I for one am not going to attempt to follow it.
Added to which (as I ask in the first sentence here) you haven't actually said what's wrong.

7. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

Lol. I could've sworn that I said that the binary to hex part doesn't work. Just nevermind. I found out how to do it, I think, w/o it being so wordy. By poorly indented, you mean by me not using enough spaces b/w lines or comments? I added the spaces b/w lines...

I have ONE more question. How can I get the two println statements to show at the same time? Right now, "Enter the Binary value to convert to Decimal:" shows first, then the next one. It may be an amateur question TO YOU, but I'm just trying to pass this class for credits... & I know little to nothing.

Java Code:
```import java.io.*;

public class BinToDecAndHex{
public static void main(String[] args) throws IOException{

System.out.print("Enter the Binary value to convert to Decimal: ");
System.out.print("Enter the Binary value to convert to Hexadecimal: ");

int bin= Integer.parseInt(number,2);
String hex = Integer.toHexString(bin);

long num = Long.parseLong(str);
long rem;
while(num > 0){
rem = num % 10;
num = num / 10;
if(rem != 0 && rem != 1)
{
System.out.println("This is not a binary number.");
}
}
int i= Integer.parseInt(str,2);
System.out.println("Decimal:="+ i);
}
}```
Last edited by manibby93; 10-09-2013 at 06:48 PM.

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

Originally Posted by manibby93
Lol. I could've sworn that I said that the binary to hex part doesn't work.
Most folks come to this forum because their code doesn't work. That's a given. What was wanted was more specific information. Like when I enter this it prints this or behaves this way.

Edit: You might also consider sprinkling some diagnostic print statements throughout your program.

Regards,
Jim
Last edited by jim829; 10-09-2013 at 07:12 PM.

9. ## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

Originally Posted by manibby93
I wrote it so that I could use it, lol. I don't know HOW to. Omg. Lol.
Have mercy... I just mixed them together. I don't know. I just want to know how to make it WORK, not be questioned about why I copied & pasted something...
My questions were aimed at getting you to think about your code, how it works and how it can be improved. I didn't mean for it to be harsh criticism.

10. Member
Join Date
Sep 2013
Posts
77
Rep Power
0

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

I'm mean, aren't I? lol. No, but seriously, how can I get both println statements to show on a separate line at the same time? I'll take trying to get me to think, lol.

11. ## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

Post an example of the output you get and the output you expect instead.

12. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: I'm trying to convert binary to decimal & hexadecimal. Almost there??

Originally Posted by manibby93
By poorly indented, you mean by me not using enough spaces b/w lines or comments? I added the spaces b/w lines...
No, I mean the indentation.
Look at this bit, the amount each line is indented is all over the place.
Java Code:
```        while(num > 0){
rem = num % 10;
num = num / 10;
if(rem != 0 && rem != 1)
{
System.out.println("This is not a binary number.");
}
}```
Code should be indented such that you can spot where a while loop ends, and which closing bracket applies to which block of code.
And the style of brackets should be consistent ie, opening brackets should either be on a new line or at the end of the previous line, not a mix.
Java Code:
```    while (num > 0) {
rem = num % 10;
num = num / 10;
if (rem != 0 && rem != 1) {
System.out.println("This is not a binary number.");
}
}```
[/QUOTE]
I have ONE more question. How can I get the two println statements to show at the same time? Right now, "Enter the Binary value to convert to Decimal:" shows first, then the next one. It may be an amateur question TO YOU, but I'm just trying to pass this class for credits... & I know little to nothing.
[/QUOTE]

I suspect (once you've clarified the problem) that you're going to be bumping up against the limitations of the command line.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•