In this section, We are going to convert a byte value into a hexadecimal number. The package java.lang provides the functionality for this conversion.
Code Description: This program takes a byte number from console as input and converts it into the hexadecimal by using the toHexString() method. The method Integer.parseInt() takes a string as input and converts it into an integer number.
Here is the code of this program
import java.io.*;
import java.lang.*;
public class ByteToHexa{
public static void main(String[] args) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the byte number:");
byte b =(byte)6;
String str = buff.readLine();
int i =Integer.parseInt(str);
String hexString = Integer.toHexString(i);
System.out.println("Hexa is:=" + hexString);
}
}
Output this program.
| C:\corejava>java ByteToHexa Enter the byte number: 31 Hexa is:=1f C:\corejava> _ |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Convert Byte to Hexadecimal View All Comments
Post your Comment