Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in the java which takes as input an integer (positive or negativ

ID: 3795562 • Letter: W

Question

Write a program in the java which takes as input an integer (positive or negative) in base 10, and returns a string representation in 32-bit of the number in hexadecimal and binary.

Use a twos-complement representation for negative numbers

You can create an array of symbols 0-F to make it easier to figure out each digit.

char digits[]=[‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’A’,’B’,’C’,’D’,’E’,’F’];

then digits[12] will return ‘C’

You should convert the absolute value to binary first, then take the twos complement if the value is negative, then convert the binary to hexadecimal

You may not use any built in conversion operators or print operators that can do the conversion automatically (i.e. NO printf(‘%x’,number)).

Write a program in the java which takes as input a string representation of an unsigned hexadecimal number in 32 bits and returns the positive integer that is the base 10 equivalent.

You do not need to worry about negative values for this question

You can use a dictionary, or brute force to convert hex digits to numbers (i.e. write a function with a bunch of if statements or a select statement to deal with digits A-F. or store the values 0-F as keys in a dictionary with the values being the number the digit represents.

For both programs, all non-decimal numbers should be assumed to be 32-bit. When inputting or outputting binary or hex, please make sure the numbers represented are full 32-bit numbers. Do not leave off the leading 0’s or 1’s.

Binary: 00001111110111010101111111011101 (32 digits)

Hex: 0FDD5FDD (8 digits long)

Explanation / Answer

Here is the code for you:

import java.util.*;
class DecimalToBinaryAndHex
{
public static void integerToBinary(int num, char[] binary)
{
binary[32] = '';
for(int i = 0; i < 32; i++)
binary[i] = '0';
int count = 31;
while(num != 0)
{
binary[count] = num % 2 == 0 ? '0' : '1';
num /= 2;
count--;
}
}
public static char binaryToHexDigit(char hex[])
{
if(hex.equals("0000"))
return '0';
else if(hex.equals("0001"))
return '1';
else if(hex.equals("0010"))
return '2';
else if(hex.equals("0011"))
return '3';
else if(hex.equals("0100"))
return '4';
else if(hex.equals("0101"))
return '5';
else if(hex.equals("0110"))
return '6';
else if(hex.equals("0111"))
return '7';
else if(hex.equals("1000"))
return '8';
else if(hex.equals("1001"))
return '9';
else if(hex.equals("1010"))
return 'A';
else if(hex.equals("1011"))
return 'B';
else if(hex.equals("1100"))
return 'C';
else if(hex.equals("1101"))
return 'D';
else if(hex.equals("1110"))
return 'E';
else if(hex.equals("1111"))
return 'F';
return ' ';
}
public static void binaryToHex(char[] binary, char[] hex)
{
hex[8] = '';
for(int i = 0; i< 8; i++)
hex[i] = '0';
char[] hexDigit = new char[5];
for(int i = 0; i < 8; i++)
{
hexDigit[0] = binary[4*i];
hexDigit[1] = binary[4*i + 1];
hexDigit[2] = binary[4*i + 2];
hexDigit[3] = binary[4*i + 3];
hexDigit[4] = '';
hex[i] = binaryToHexDigit(hexDigit);
}
}
public static void main(String[] args)
{
int number;
System.out.printf("Enter an integer: ");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
char[] binary = new char[33];
char[] hex = new char[9];
integerToBinary(number, binary);
binaryToHex(binary, hex);
//System.out.printf("%d = 0b %s = 0x %s ", number, binary, hex);
System.out.printf("%d = ", number);
for(int i = 0; i < 32; i++)
System.out.printf("%c", binary[i]);
System.out.printf(" ");
for(int i = 0; i < 8; i++)
System.out.printf("%c", hex[i]);
System.out.println();
}   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote