Conversions in Java
Conversions in Java: Data Representation
Statement Purpose:
This lab will give you practical implementation of conversions and arithmetic on unsigned and signed integers and real numbers among different radices of decimal, binary, and hexadecimals using programs in JAVA.
Activity Outcomes:
This lab teaches you the following topics:
- Conversions between different
- Representations of signed and unsigned integer
- Representation of floating pint
- Arithmetic operations on numbers of all
Instructor Note:
As pre-lab activity, read Chapter 1 section 1.3 from the book (Assembly Language for X86 processors, KIP R. IRVINE., 7th Edition (2015), Pearson), and also as given by your theory instructor.
Introduction
Everything suited for processing with digital computers is represented as a sequence of 0s and 1s, whether it be numeric data, text, executable files, images, audio, or video. The meaning of a given sequence of bits within a computer depends on the context. In this section we describe how to represent integers in binary, decimal, and hexadecimal and how to convert between different representations. We also describe how to represent negative integers and floating-point numbers
Activities:
Activity 1:
Hexadecimal to decimal and binary conversion, This Java program will convert Hexadecimal number to decimal, binary and Octal in Java programming language using JDK standard API methods. For beginner’s hexadecimal is base 16 number, while decimal is base 10, Octal is base 8 and binary is base 2 numbers in Number systems. Binary only contains 0 and 1 bits, octal contains 0 to 7, decimal contains 0 to 9 and Hexadecimal contains 0-9, A, B, C, D, E, F where F represent 16. Thankfully Java library provides convenient method to convert any integer fromone number system to another.
Solution:
Java API provides two methods which is used in converting number from one number system to other. One is Integer.parseInt() which is used tobut also allows you to specify radix.
- Java program to convert Hexadecimal to binary, decimal and Octal in
- Hexadecimal is base 16, Decimal number is base 10, Octal is base 8
- and Binary is base 2 number which has just two numbers 0 and
- @author
*/
public class ConvertHexaToDecimal { public static void main (String args[]) {
// Ask user to enter an Hexadecimal number in Console System.out.println(“Please enter Hexadecimal number : “); Scanner scanner = new Scanner(System.in);
String hexadecimal = scanner.next();
//Converting Hexa decimal number to Decimal in Java int decimal = Integer.parseInt(hexadecimal, 16);
System.out.println(“Converted Decimal number is : ” + decimal);
//Converting hexa decimal number to binary in Java String binary = Integer.toBinaryString(decimal);
System.out.printf(“Hexadecimal to Binary conversion of %s is %s %n”, hexadecimal, binary );
// Converting Hex String to Octal in Java String octal = Integer.toOctalString(decimal);
System.out.printf(“Hexadecimal to Octal conversion of %s is %s %n”, hexadecimal, octal );
}
}
Output:
Please enter Hexadecimal number :
A
Converted Decimal number is : 10
Hexadecimal to Binary conversion of A is 1010
Hexadecimal to Octal conversion of A is 12
Activity 2:
Example shows how to convert binary to decimal in JAVA. Solution:
- Create Java Main Class and named it
- Copy the Code and test it by running the application
import java.util.Scanner; public class Binary_Decimal {
Scanner scan; int num;
void getVal() { System.out.println(“Binary to Decimal”); scan = new Scanner(System.in);
System.out.println(“\nEnter the number :”); num = Integer.parseInt(scan.nextLine(), 2);
}
void convert() {
String decimal = Integer.toString(num); System.out.println(“Decimal Value is : ” + decimal);
}
}
class MainClass {
public static void main(String args[]) { Binary_Decimalobj = new Binary_Decimal(); obj.getVal();
obj.convert();
}
}
Activity 3:
The example below, how to convert from binary to Octal in JAVA.
import java.util.Scanner; public class Binary_Octal {
Scanner scan; int num;
void getVal() {
System.out.println(“Binary to Octal”); scan = new Scanner(System.in);
System.out.println(“\nEnter the number :”); num = Integer.parseInt(scan.nextLine(), 2);
}
void convert() {
String octal = Integer.toOctalString(num); System.out.println(“Octal Value is : ” + octal);
}
}
class MainClass {
public static void main(String args[]) { Binary_Octalobj = new Binary_Octal(); obj.getVal();
obj.convert();
}
}
Activity 4:
Here it is demonstrated how to convert from binary to hexadecimal.
Solution:
import java.util.Scanner; public class Binary_Hexa {
Scanner scan; int num;
void getVal() {
System.out.println(“Binary to Hexadecimal”); scan = new Scanner(System.in); System.out.println(“\nEnter the number :”); num = Integer.parseInt(scan.nextLine(), 2);
}
void convert() {
String hexa = Integer.toHexString(num); System.out.println(“Hexadecimal Value is : ” + hexa);
}
}
class MainClass {
public static void main(String args[]) { Binary_Hexaobj = new Binary_Hexa(); obj.getVal();
obj.convert();
}
}
Activity 5:
Here your job is to write a function that takes an integer input and returns.
Solution:
public static intbitCount(int input) { int count = 0;
for (int i = 0; i< 32; i++)
count = count + (input >>>i& 1); return count;
}
public static intbitCount(int x) { if (x == 0) return 0;
return (x & 1) + bitCount(x >>> 1);
}
This is how Integer.bitCount() is implemented by Java. See if you can figure out how it works.
public static intbitCount(int i) { i = i – ((i>>> 1) & 0x55555555);
i = (i& 0x33333333) + ((i>>> 2) & 0x33333333);
i = (i + (i>>> 4)) & 0x0f0f0f0f; i = i + (i>>> 8);
i = i + (i>>> 16);
return i& 0x3f;
}
Stage v(verify)
Home Activities:
Activity 1:
Covert the following programs Pseudo-code in JAVA.Just use the algorithms given in activity.
- Conversion from base 2 and base 16 numbers to Decimal
- Conversion from decimal number to base 2 and base 16
- Conversion of ay Binary fraction to Decimal
- Conversion of decimal Fraction to Binary
- Decimal number to Floating Point Binary Representation