Question

I am trying to write code in java for decoding in the arithmetic binary code. The problem is the binary number I received is, for example, "0110111100101011", representing the binary fractional value 0.011011110010101, and equal to 0.4342498 decimal.

I couldn't find any way to do this; please help me.

The string that I know the equation and how to calculate after the value of the string is converted into a double value, but I do not know how to convert the string into double.

import java.util.Scanner;

double value = .434249878; // still cant kno who to chamge it from binary to decimal
double upper = 1.0;
double lower = 0.0;
double current_range  = upper  - lower;
char ch;
double v;
Scanner scan=new Scanner (System.in);
System.out.println ("Enter characor");
    //  ch= scan.next().charAt(0);

while(ch!='n'){
current_range = upper - lower;
v= (value-lower) / (upper-lower);
Was it helpful?

Solution

A simpler approach is to parse the binary number and apply the exponent last.

public static double parseBinary(String s) {
    int exp = Integer.MIN_VALUE;
    long value = 0;
    for(int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
        if (ch == '.') {
            exp = 0;
        } else {
            value = value * 2 + ch - '0';
            exp++;
        }
     }
     return exp < 0 ? value : Math.scalb(value, -exp); // value * 2^^-exp
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top