1

I need to write a program that can convert bits into decimal. Whenever I enter a bit, it only outputs 0.0. I cannot figure out why. I know it's incredibly simple but I am just not seeing it. Any help would be appreciated.

import java.lang.Math;
import java.util.Scanner;


public class Lab1 {
    static double number = 0;

    public static double toDec(String num) {

        char[] charArray = num.toCharArray();

        for(int i = 0; i<charArray.length;i++) {
            if(charArray[i] == 1) {
                number = Math.pow(2, charArray.length-i);
            }
        }

        return number;


    }


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int bit;
        String bitString;

        System.out.println("Please enter a bit");
        bit = keyboard.nextInt();
        bitString = Integer.toString(bit);


        System.out.println(toDec(bitString));



    }

}
2
  • try to use a debugger for this, you will figure it out quickly that the statement inside the if(charArray[i] == 1) is not getting executed .... Commented Sep 5, 2013 at 19:05
  • 2
    You might be interessed to know that Integer.parseInt("0100010101",2) does the job. Commented Sep 5, 2013 at 19:09

3 Answers 3

6

You have compared charArray[i] to 1, but you're comparing apples to oranges, specifically, a char to an int.

Compare to the char '1' instead.

if(charArray[i] == '1') {

Also, you can make number a local variable in toDec; it doesn't need to exist outside that method.

In addition, this will only work if one bit is set. Right now you are working with one bitonly, but if you want to modify this to work with multiple bits, another changes is needed.

You overwrite number each time toDec is called and the condition is true. You will probably want to add to number with += instead of overwriting the previous value with =.

Sign up to request clarification or add additional context in comments.

5 Comments

chars have an int value, so it is not quite an apple to orange comparison. but it is definitely not what he wants.
Thank you so much. It is working now. I really appreciate it!
@ColinD Everything has an int value. But if you want to say comparing an int to a HashMap is not as bad as than an apple to an orange, we have a problem lol.
@user2002573 charArray.length-i is 1 too big. Use charArray.length-i-1.
@user2002573 You have an off-by-one problem. Let's say you have "001", length 3. By the time you get to the end of the array, i is 2, and 3 - 2 = 1. The result of pow multiplies it by 2. Subtract 1, i.e. charArray.length - i - 1.
1

Integer#parseInt(String str, int radix) does the job :

public static Integer toDec(String num) {
  return Integer.parseInt(num, 2);
}

Comments

0

So if you want to take the String "110011" which is 51. For big-endian you are going to have to determine how many bits to process. So if you read the string and it is 6 digits long then you know the first bit has to be shifted 6 places to the left.

int l = 6;
long value = 0;

for( int i = 0; i < l; i++ )
{
   int bit = ( charArray[i] == "1" ) ? 1 : 0;

   l = l + ( bit << l-i );
}

For float you would basically have to build an interpreter to decode the bits based on however the float is represented in binary.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.