Jump to content

Help with binary to decimal

Go to solution Solved by Guest,

It is java, there is a tag at the top. 

If I out in 101 it does 5 correctly, but if I put in 110001 (49) It will return 35 which is 100011. I belive this is the cause for my other problem which is it ignores 0's at the begining or end, making it return 1 if I put in 100.

 

Ok. How's this:

public String binaryToDecimal(int binary) {  result = 0;  String binaryString = String.valueOf(binary);  for (int i = 0; i < binaryString.length(); i++) {    if (binaryString.charAt(i) == '1') {       result += pow(2, (binaryString.length() - 1) - i);    }  }  return Integer.toString(result);}

I think the problem is that you are going through the string from left to right, but the least significant bit is on the right.

I am trying to make a program that will change binary numbers to decimal, but it is not working and I am not sure why. It seems to be reversing the order of the input and therefore ignores 0s if it ends with one. I have the opposite of this working with the same square method, so I think it is just a problem with the conversion.
 
 
 
public void calc(int a) {
output1 = 0;
String input = String.valueOf(a);
String in[] = new String[input.length()];
for(int k = 0; k < input.length(); k++) {
in[k] = input.substring(k, k + 1);
}
for(int i = 0; i < input.length(); i++) {
if(in.equalsIgnoreCase("1")) {
int j = square(i, 2);
output1 = output1 + j;
}
}
label1.setText(Integer.toString(output1));
input1 = 0;
}
 
public int square(int a, int b) {
if(a == 0) return 1;
int total = b;
for(int i = 0; i < a - 1; i++) {
total = total * b;
}
return total;
}

 

http://pcpartpicker.com/p/9DNjLk I like the night theme, it complements my dark basement. If you are reading this then you must also be a fan of the night theme.

01011011 01000010 01101001 01101110 01100001 01110010 01111001 00100000 01001000 01100101 01110010 01100101 01011101 

Link to comment
https://linustechtips.com/topic/316144-help-with-binary-to-decimal/
Share on other sites

Link to post
Share on other sites

For readability.

 

public void calc(int a) {  output1 = 0;  String input = String.valueOf(a);  String in[] = new String[input.length()];  for (int k = 0; k < input.length(); k++) {    in[k] = input.substring(k, k + 1);   }  for (int i = 0; i < input.length(); i++) {    if (in[i].equalsIgnoreCase("1")) {       output1 += square(i, 2);    }  }  label1.setText(Integer.toString(output1));  input1 = 0;}public int square(int a, int b) {  if (a == 0) return 1;  int total = b;                                                                      for (int i = 0; i < a - 1; i++) {    total *= b;  }  return total;}
Link to post
Share on other sites

So I'm not entirely sure what language this is, I'm guessing C++?

 

Can you give some example inputs and the output that you are getting with the code as it is? I don't really understand why you are squaring anything as binary is based on a power-of-two system.

Link to post
Share on other sites

So I'm not entirely sure what language this is, I'm guessing C++?

 

Can you give some example inputs and the output that you are getting with the code as it is? I don't really understand why you are squaring anything as binary is based on a power-of-two system.

It is java, there is a tag at the top. 

If I out in 101 it does 5 correctly, but if I put in 110001 (49) It will return 35 which is 100011. I belive this is the cause for my other problem which is it ignores 0's at the begining or end, making it return 1 if I put in 100.

http://pcpartpicker.com/p/9DNjLk I like the night theme, it complements my dark basement. If you are reading this then you must also be a fan of the night theme.

01011011 01000010 01101001 01101110 01100001 01110010 01111001 00100000 01001000 01100101 01110010 01100101 01011101 

Link to post
Share on other sites

It is java, there is a tag at the top. 

If I out in 101 it does 5 correctly, but if I put in 110001 (49) It will return 35 which is 100011. I belive this is the cause for my other problem which is it ignores 0's at the begining or end, making it return 1 if I put in 100.

 

Ok. How's this:

public String binaryToDecimal(int binary) {  result = 0;  String binaryString = String.valueOf(binary);  for (int i = 0; i < binaryString.length(); i++) {    if (binaryString.charAt(i) == '1') {       result += pow(2, (binaryString.length() - 1) - i);    }  }  return Integer.toString(result);}

I think the problem is that you are going through the string from left to right, but the least significant bit is on the right.

Link to post
Share on other sites

Ok. How's this:

public String binaryToDecimal(int binary) {  result = 0;  String binaryString = String.valueOf(binary);  for (int i = 0; i < binaryString.length(); i++) {    if (binaryString.charAt(i) == '1') {       result += pow(2, (binaryString.length() - 1) - i);    }  }  return Integer.toString(result);}

I think the problem is that you are going through the string from left to right, but the least significant bit is on the right.

 

Your code helped me figure out what I did. I was using 2 for loops, leaving a lot more area for errors. I made your code fit into my program and it works fine now. Thanks

http://pcpartpicker.com/p/9DNjLk I like the night theme, it complements my dark basement. If you are reading this then you must also be a fan of the night theme.

01011011 01000010 01101001 01101110 01100001 01110010 01111001 00100000 01001000 01100101 01110010 01100101 01011101 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×