The Artima Developer Community
Sponsored Link

Java Answers Forum
Converting integers and binary numbers

5 replies on 1 page. Most recent reply: Nov 16, 2002 6:30 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Althea Spriggs

Posts: 1
Nickname: thea22
Registered: Nov, 2002

Converting integers and binary numbers Posted: Nov 12, 2002 7:51 AM
Reply to this message Reply
Advertisement
I need assistance writing a program to convert integers from decimal to binary and hexadecimal, and to convert binary numbers to decimal and hexadecimal. I have to use 8 bit signed representation(-128 and 127). Binary integers should be in 2's complement form.

The program has to accept input from the keyboard and display results on the output screen.

This is my sample interface

c:/>NumConv

Select one of the following:

B Convert from Binary to Decimal and Hexadecimal
D Convert from Decimal to Binary and Hexadecimal
X Exit

D

Enter a Decimal Integer between -128 and 127
15
15 decimal is 00001111 binary or 0f hexadecimal

B Convert from Binary to Decimal and Hexadecimal
D Convert from Decimal to Binary and Hexadecimal
X Exit

B

Enter a Signed Binary Integer
10011011
100 110 11 is -101 decimal or 9B hexadecimal

The data structures and other items are causing me headaches.


Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: Converting integers and binary numbers Posted: Nov 12, 2002 8:55 AM
Reply to this message Reply
there is a method in converting integer to binary and vice versa in Java API try to find it and use it. if you have a problem go back here.

i will help you, but first try first Ok.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting integers and binary numbers Posted: Nov 12, 2002 11:42 AM
Reply to this message Reply
In the Integer class API:
toBinaryString
public static String toBinaryString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 2.

toHexString
public static String toHexString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 16.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting integers and binary numbers Posted: Nov 12, 2002 11:44 AM
Reply to this message Reply
to convert back use:
parseInt
public static int parseInt(String s,int radix)

for binary:

parseInt("1100110", 2) returns 102
for hex:

parseInt("FF", 16) returns 255

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting integers and binary numbers Posted: Nov 12, 2002 11:48 AM
Reply to this message Reply
Integer.toBinaryString(102);
should produce 1100110

Integer.toHexString(255);
should produce ff

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting integers and binary numbers Posted: Nov 16, 2002 6:30 PM
Reply to this message Reply

import java.io.*;
public class NumberConverter{

public NumberConverter(){

}


public static void main(String[] args){
NumberConverter converter = new NumberConverter();
boolean done = false;
while (!done){
String selection = converter.getSelection();
selection = selection.trim().toUpperCase();
char c = selection.charAt(0);
if (c == 'B'){
String numberString = converter.getEntry("Enter a Signed Binary Integer");
byte n = (byte)Integer.parseInt(numberString, 2);
String hexString = Integer.toHexString(Integer.parseInt(numberString, 2)).toUpperCase();
while (hexString.length() < 2){
hexString = "0" + hexString;
}
System.out.println(numberString
+ " is " + String.valueOf(n) + " decimal or "
+ hexString + " hexadecimal.");
}else if (c == 'D'){
String numberString = converter.getEntry("Enter a Decimal Integer between -128 and 127");
int n = Integer.parseInt(numberString);
String binaryString = Integer.toBinaryString(n);
while (binaryString.length() < 8){
binaryString = "0" + binaryString;
}
String hexString = Integer.toHexString(n).toUpperCase();
while (hexString.length() < 2){
hexString = "0" + hexString;
}
System.out.println(numberString
+ " decimal is " + binaryString + " binary or "
+ hexString + " hexadecimal.");
}else if (c == 'X'){
done = true;
}else{
System.out.println("Unknown Option: Try again.");
}
}
}

public String getSelection(){
String menu = "Select one of the following:\n"
+ "\n"
+ "B Convert from Binary to Decimal and Hexadecimal\n"
+ "D Convert from Decimal to Binary and Hexadecimal\n"
+ "X Exit\n";
System.out.println(menu);
String userInput = "";
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
userInput = br.readLine();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return userInput;
}

public String getEntry(String message){
System.out.println("\n" + message);
String userInput = "";
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
userInput = br.readLine();
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
return userInput;
}
}

Flat View: This topic has 5 replies on 1 page
Topic: where is the best that could help solving this script Previous Topic   Next Topic Topic: String vs StringBuffer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use