Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 21st, 2013, 01:55 PM
Darksole Darksole is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 Darksole User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
Binary to decimal conversion java noob here

hey guys new to the forum, and a noob at java. i am taking a java class and am stuck. below it the code i have atm

Code:
import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Binary {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
// enter binary
String s = scan.next();

int decimalValue = Integer.parseInt(s, 2);

// print decimal version of binary
System.out.println(decimalValue);

// print ascii version of binary
System.out.println((char) decimalValue);
// print 256 minus decimal
System.out.println(256 - decimalValue);
	}
}


this code converts a imputed 8bit binary value such as 01100001 and returns the decimal form the ascii form and subtracts the decimal from 256. i have been told however i can not use parseInt to do this. i have to use the scanner class to read the input, and i cannot use if while or for statements. i am allowed to use string class methods.

i am lost i know the conversion uses a format such as (0*32)+(0*16) + (1*8) + (0*4) + (0*2) +( 0*1).

any help converting the above code to use a imputed 8bit binary value would be appreciated thank you

Reply With Quote
  #2  
Old January 30th, 2013, 03:55 PM
davidhaibk davidhaibk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 davidhaibk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by Darksole
hey guys new to the forum, and a noob at java. i am taking a java class and am stuck. below it the code i have atm

Code:
import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Binary {
public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
// enter binary
String s = scan.next();

int decimalValue = Integer.parseInt(s, 2);

// print decimal version of binary
System.out.println(decimalValue);

// print ascii version of binary
System.out.println((char) decimalValue);
// print 256 minus decimal
System.out.println(256 - decimalValue);
	}
}


this code converts a imputed 8bit binary value such as 01100001 and returns the decimal form the ascii form and subtracts the decimal from 256. i have been told however i can not use parseInt to do this. i have to use the scanner class to read the input, and i cannot use if while or for statements. i am allowed to use string class methods.

i am lost i know the conversion uses a format such as (0*32)+(0*16) + (1*8) + (0*4) + (0*2) +( 0*1).

any help converting the above code to use a imputed 8bit binary value would be appreciated thank you

Hope this help:

Code:

import java.util.Scanner;
import java.lang.Integer;
import java.lang.String;
public class Binary {
public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		
		while (scanner.hasNext()) {
			String s = scanner.next();
			
			//8 bits
			if(s.length() != 8){
				System.out.println("Wrong binary number!");
				continue;
			}
			
			// matching 8-bit binary 
			if(s.matches("[01]+")){
				int decimal = passInt(s);
				System.out.println("number = "+ decimal);
				System.out.println("char = "+ (char)decimal);
				System.out.println(256 - decimal);
			}else{
				System.out.println("Wrong binary number!");
			}	
		}
	}
	
	public static int passInt(String s){	
		int result = 0;
		for(int i = 0; i< s.length(); i++){			
			result += Integer.parseInt(Character.toString(s.charAt(i)))*  Math.pow(2,i);			
		}
		return result;
	}
	
	
}

Reply With Quote
  #3  
Old February 23rd, 2013, 07:50 PM
burakaltr burakaltr is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 burakaltr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 55 sec
Reputation Power: 0
Code:
import java.util.Scanner;
import java.math.*;
public class baseConvert {
    public static void main(String[] args) 
    {
            double tab2,num,onluk,num2,tabson,
           k;
                      double tab1,i;
            
            double[] dig=new double[100];
            double[] tab2lik=new double[100];
            int[] tab2likint=new int[100];
while(true){
            Scanner in = new Scanner(System.in);
            System.out.println("  ");

                         System.out.println(" enter base 1 ");
                         tab1 = in.nextDouble();

                         
                         System.out.println(" enter base 2 ");
                         tab2 = in.nextDouble();       
                         
                         
                         System.out.println(" enter number 1 ");
                         num = in.nextDouble();
                         for(int i1=0;i1<=19;i1++)
                         {
                        	 tab2lik[i1]=0;dig[i1]=0;
                         }
                         onluk=0;
                         num2=num;
                         k=0;
                         for(int i1=0;i1<=19;i1++)
                         {
                        	 k=k+1;
                        	dig[i1]=num % 10;
                        	//if(dig[i1]<0){dig[i1]=dig[i1]+10;}
                        	num=(num-dig[i1])/10;
                        	
                        	
                            

                    //    System.out.println(dig[i1]);
                         }
                         
                    //   onluk=dig[k]*tab1^k;
                      
                         onluk=0;
                    // for(k=0;k<=50;k++){
                        // }
                         i=0;
                         for(int i1=0;i1<=k;i1++)
                         {
                        	 i=Math.pow(tab1,i1);
                        	/* System.out.println(i);
                        	*/ 
                        	 
                         	 onluk=dig[i1]*(i)+onluk;
                            
                        	
 //
                         	 
                         }   
                         if(tab1==10){
                    		 onluk=num2;
                    	 }
                    	 if(num2<10){
                     		onluk=num2;
                     }
                    	// System.out.println(onluk);
                         tabson=0;
 
                         for(int i1=0;i1<=k;i1++)
                         {
                        	 tab2lik[i1]=onluk % tab2;
                      //  if(tab2lik[i1]<0)
                        //{
                        // tab2lik[i1]=tab2lik[i1]+tab2;
                        //}
                        	 onluk=((onluk-tab2lik[i1])) 
                    ;
                        	 onluk=onluk/tab2;
                        //  System.out.println(tab2lik[i1]);
                        // tabson=tabson+tab2lik[i1]*(10^i1);
                         
                      //   System.out.println(tabson);
                         
                         }  	 
   
                       tabson=0;
                       System.out.println(num2+" in base "+tab1+" = "+" in base "+ tab2+" equals the number below :");
                  	    
                         
                     for( int i1=50;i1>=0;i1--)
                        {
                       //
                    	//tabson=tabson+tab2lik[i1]*(10^i1);
                    	  tab2likint[i1]= (int) tab2lik[i1];
                    	 
                    	 System.out.print("" + tab2likint[i1] );
                        	 
                       //    System.out.println(tabson);
                        }  	 
                           //  System.out.println(num2+" in base "+tab1+" = "+ tabson+ " in base "+ tab2);
                            }}}             

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Binary to decimal conversion java noob here


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap