I made a program which converts binary with ft pt but cant get it work, what could be wrong here?

import java.io.*;
import java.util.Scanner;

public class NumberConverter
{
	public static void main(String args[])throws Exception
	{
		Scanner sc = new Scanner(System.in);		
		System.out.print("Enter a number: ");		
		String string_input = sc.nextLine();
		
		double double_input = Double.parseDouble(string_input);
		
		int int_input = (int)double_input;		
		float float_input = (float)(double_input - int_input);
			
		int int_remainder;		
		float float_remainder;
		
		String int_output = "";		
		String float_output = ".";
		
		do
		{
			int_remainder = int_input % 2;
			int_output = Integer.toString(int_remainder) + int_output;
			int_input /= 2;			
		}while(int_input > 0);
		
		do
		{
			float_remainder = float_input * 2;
			
			if(float_remainder < 1)
			{
				float_output += "0";
			}
			else
			{
				float_output += "1";
				float_input = float_remainder - 1.0f;
			}
			
			float_input *= 2;
			
		}while(float_input > 0);
		
		System.out.println(int_output + float_output);
	}
}

Recommended Answers

All 8 Replies

What doesn't work exactly? Is the conversion not as you intended or what?

the whole number works while the floating point doesnt. maybe theres something that isnt right with my code in the body of 2nd do while and / or the condition of my 2nd do while. can you see whats the problem here?

Member Avatar for ztini

The problem is your logic on the float.

.1111_2 = 2^-1 + 2^-2 + 2^-3 + 2^-4 = .9375_10

So change your do-while to this:

int bitLocation = 1;
		while (float_input > 0 && bitLocation <= 24) {
		
			if (float_input - 1 / (Math.pow(2, bitLocation)) >= 0) {
				float_output += "1";
				float_input -= 1 / (Math.pow(2, bitLocation));
			}
			else
				float_output += "0";
			
			bitLocation++;
		};

This assumes 32-bit output (8 + 24).

i think it works, but i am not sure if how many floating point digits it can return. example: if I enter 10.1 it only returns 1010.0001, but i want the whole conversion such as 1010.0001100110011001100110011001100110011001100110011 and if I enter 99.9 it only gives me 1100011.1 but when I tried calculating it manually it should be 1100011.111001100110011001100110011001100110011001101. I am not sure if I how to do it now. Thanks though for the math.pow I got an idea with that recommendation.

Member Avatar for ztini

Post your revised code. I tested it with the above while loop and it worked perfectly for me. Using that loop you can return as many digits as you want, but you probably only want to return 24 plus 8 on the left side of the decimal to create a 32 bit floating point. Also, keep in mind with IEEE754, you need to indicate the sign and mantissa.

import java.io.*;
import java.util.Scanner;

public class NumberConverter
{
	public static void main(String args[])throws Exception
	{
		Scanner sc = new Scanner(System.in);		
		System.out.print("Enter a number: ");		
		String string_input = sc.nextLine();
		
		double double_input = Double.parseDouble(string_input);
		
		int int_input = (int)double_input;		
		float float_input = (float)(double_input - int_input);
			
		int int_remainder;		
		float float_remainder;
		
		String int_output = "";		
		String float_output = ".";
		
		int bitLocation = 1;
		
		do
		{
			int_remainder = int_input % 2;
			int_output = Integer.toString(int_remainder) + int_output;
			int_input /= 2;			
		}while(int_input > 0);
		
		while(float_input > 0 && bitLocation <= 24)
		{
			//float_remainder = float_input * 2;
			
			if(float_input - 1 / (Math.pow(2, bitLocation)) >= 0)
			{
				float_output += "1";
				float_input -= (Math.pow(2, bitLocation));
			}
			else
			{
				float_output += "0";
				//float_input = float_remainder - 1.0f;
			}
			
			bitLocation++;
			
			//float_input *= 2;
			
		}
		
		System.out.println(int_output + float_output);
	}
}
Member Avatar for ztini

line 39 should read:

float_input -= [B]1 / [/B](Math.pow(2, bitLocation));

ztni, its solved using your solution. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.