Get Binary Numbers

how to get binary numbers?

Page 1 of 1

11 Replies - 14154 Views - Last Post: 25 March 2010 - 04:27 PM Rate Topic: -----

#1 mrfikri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 12-October 08

Get Binary Numbers

Post icon  Posted 05 January 2009 - 01:05 AM

sory, I can't post any code as my sample... i don't have any idea on how to get binary number from any character that we entered through java. i'm using jdk 6.0 :blink:
Is This A Good Question/Topic? 0
  • +

Replies To: Get Binary Numbers

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Get Binary Numbers

Posted 05 January 2009 - 01:07 AM

Character entered where?
Was This Post Helpful? 0
  • +
  • -

#3 mrfikri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 12-October 08

Re: Get Binary Numbers

Posted 05 January 2009 - 01:31 AM

View Postn8wxs, on 5 Jan, 2009 - 12:07 AM, said:

Character entered where?


maybe, it'll be somthing like this...

 public static void main(String[] args)
	{
		int[] intList = new int[3];
		int index;

		System.out.println("Line 4: Enter 3 integers.");

		for (index  = 0; index < 3; index++)
			intList[index] = console.nextInt();
	 }


Every data insert into arrays, i want to convert it to binary numbers. do u know how?
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Get Binary Numbers

Posted 05 January 2009 - 01:37 AM

Have a look at Class Scanner
Was This Post Helpful? 1
  • +
  • -

#5 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Get Binary Numbers

Posted 05 January 2009 - 01:54 AM

Here is how you can do it in java by using toBinaryString() function:
	int n; in this case n is an integer

	String by = Integer.toBinaryString(n);  //to convert integer to binary

	String hex = Integer.toHexString(n); //to convert integer to hexadecimal
	 
	String oct = Integer.toOctalString(n);  //to convert integer to octal



hope that helps, good luck in your program ;)
Was This Post Helpful? 1
  • +
  • -

#6 mrfikri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 12-October 08

Re: Get Binary Numbers

Posted 15 January 2009 - 08:02 AM

thanks ayman_mastermind, :)

it helps me a lot... but what should i do if i don't want my system to only convert integer. i want it to convert characters too. :blink:
Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Get Binary Numbers

Posted 15 January 2009 - 08:12 AM

A character is represented by a number, it's called an ascii-value and is a 7-bit binary number. So what you should do if you wanna convert a character is to determine its ascii-value and then you convert that number into binary.
Was This Post Helpful? 0
  • +
  • -

#8 ProGraM  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Get Binary Numbers

Posted 15 January 2009 - 08:49 AM

here Edited Code

import java.io.*;
import javax.swing.JOptionPane;
public class Decimal 
{
	   static String[] Bases = {"Binary"};//creates a string array to show the types of base conversions
	   static String number;//creates a string for the user input
	   static int value;
	   static int remainder[] = new int[15];
	public static void main(String[] args) throws IOException
	{
		Initialize();
	}
	public static void Initialize() throws IOException
	{
	   BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
			   for(int i = 0; i < 15; i++)
			   remainder[i] = -1;
			   try
			   {
			   System.out.println("\nPlease Enter A Number To Be Converted: ");
			   number = keyboard.readLine().trim();//reads user input
			   number = number.replace(" ", "");//replaces all the spaces
			   value = Integer.parseInt(number);//converts the string 'number' to an integer 'value'
			   }catch(NumberFormatException e)//catches any illegal symbols
			   {
				   JOptionPane.showMessageDialog(null,"Invalid Symbols.","WARNING",JOptionPane.WARNING_MESSAGE);//gives the user a warning message
				  System.exit(0);
			   }catch(NullPointerException e)//blocks any ^(letter) used to blow up programs
			   {
				 JOptionPane.showMessageDialog(null,"No Invalid Symbols","WARNING",JOptionPane.WARNING_MESSAGE);//gives the user a warning message
				 System.exit(0);
			   }
			   if(value >= 0 && value <= 999999)//if the value is greater than zero and less than 999999
			   {
				   System.out.println("Here is a list of what your number will be converted to: ");
				/*Shows the list of what your number will be converted to*/
				for(int list = 0; list < Bases.length; list++)
				{
					System.out.println(Bases[list] + " ");
				}	
				/*Shows all three conversions*/   
				System.out.println("\nConversions: ");
					Binary(value);
			   } 
			   
			   if(value < 0 || value > 999999)//if the value is less than zero or greater than 999999
			   {
				   JOptionPane.showMessageDialog(null,"No Negatives or Numbers Above 999999.","WARNING",JOptionPane.WARNING_MESSAGE);//gives the user a warning message
				   System.exit(0);
			   }			 
	}
	
	/*Converts the number into a Binary System*/
	private static void Binary(int value)
	{
		int binaryConversion[] = new int [31];
		boolean HitOne = false;
		String binary = "";		
		binaryConversion [0] = value / 2;	   
		for (int count = 1; count < 31; count++)
		{
			binaryConversion [count] = binaryConversion [count - 1] / 2;
		}
		System.out.print ("Binary Conversion: " + binary);
		for (int count = 30; count >= 0; count--)
		{
			if (binaryConversion [count] % 2 == 1 && !HitOne)
			{
				System.out.print (binaryConversion [count] % 2);
				HitOne = true;
			}
			else if ((binaryConversion [count] % 2 == 0 && HitOne) || binaryConversion [count] % 2 == 1)
			{
				System.out.print (binaryConversion [count] % 2);
			}
		}
		System.out.print (value % 2);
	}
}

This post has been edited by ProGraM: 15 January 2009 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#9 mrfikri  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 12-October 08

Re: Get Binary Numbers

Posted 15 January 2009 - 10:14 AM

hey guys!

thanks a lot for your help. i did it! :D

Gloin, your tips helped me to found the solution. ;) tq! and u guys sample codes really help me to start manipulating them. i can't thanks u guys ryte now. i don't know why... but, it's enough for me to say thanks :)

THUMBS UP!! :^:
Was This Post Helpful? 0
  • +
  • -

#10 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Get Binary Numbers

Posted 15 January 2009 - 10:30 AM

no problemo! Just ask if you got more questions.
Was This Post Helpful? 0
  • +
  • -

#11 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Get Binary Numbers

Posted 15 January 2009 - 10:53 AM

@mrfikri, your welcome! ;)
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Almighty*


Reputation:

Re: Get Binary Numbers

Posted 25 March 2010 - 04:27 PM

yea i think way is longer and way more complicated for some people. What about my way?

List<String> lis= new ArrayList<String>();
double a = Math.Pow(2, whatever);
String s=Integer.toBinaryString(a);
lis.add(s);
System.out.Println(lis);

You guy could change it to be input-able and it basically the same as ProGraM one.

It's just a share anyway!
Was This Post Helpful? 0

Page 1 of 1