Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

conversion from binary to decimal numbers as string

Thu Jul 19, 2012 3:53 pm

..my problem is it can't read back...it is a decimal to binary conversion, the decimal to binary conversion is OK it will run.. but the binary to decimal its not working sample i input 1010 the result is 1111110010 supposed to be it is a 10...soory for my English


Code:

import java
.util.Scanner;

class binaryLoop {
  public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please enter Integer");

int number = sc.nextInt();

String bin=bin1(number);


System.out.println("Binary number is: "+number+"    Decimal number: " +  bin);

  }

public static String bin1 (int num)
 {
int sum=1;
int temp=0;
String bin = "";
if (num==2) ;
while (num!=0)
 {
bin=(num%2)+ bin;
 num=num/2;

               }
return bin;
}
}
 




Re: BInary conversion

Fri Jul 20, 2012 8:49 pm

there is no condition for conversion from binary to decimal , you only wrote a function to convert to decimal to binary.
You will need to write another one to convert from binary to decimal where you will sum multiply of 2 power N (Position in string).

Post a reply
  Related Posts  to : conversion from binary to decimal numbers as string
 convert binary number to decimal     -  
 add sequence of decimal numbers     -  
 convert string into binary     -  
 .dll conversion     -  
 Number Conversion     -  
 Type Conversion     -  
 convert hexadecimal to decimal     -  
 please get me the following Delphi to Java conversion     -  
 Scale double number using big decimal     -  
 ramp Counter ANALOGUE TO DIGITAL CONVERSION (ADC)     -  

Topic Tags

Java Strings