Get Binary Numbershow to get binary numbers?
Page 1 of 1
11 Replies - 10516 Views - Last Post: 25 March 2010 - 04:27 PM
#1
Get Binary Numbers
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
Replies To: Get Binary Numbers
#3
Re: Get Binary Numbers
Posted 05 January 2009 - 01:31 AM
n8wxs, 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?
#5
Re: Get Binary Numbers
Posted 05 January 2009 - 01:54 AM
Here is how you can do it in java by using toBinaryString() function:
hope that helps, good luck in your program
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
#6
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.
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.
#7
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.
#8
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
#9
Re: Get Binary Numbers
Posted 15 January 2009 - 10:14 AM
hey guys!
thanks a lot for your help. i did it!
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!!
thanks a lot for your help. i did it!
Gloin, your tips helped me to found the solution.
THUMBS UP!!
#10
Re: Get Binary Numbers
Posted 15 January 2009 - 10:30 AM
no problemo! Just ask if you got more questions.
#12 Guest_Almighty*
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!
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!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|