- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a byte to hexadecimal equivalent in Java
To convert a byte to hexadecimal equivalent, use the toHexString() method in Java.
Firstly, let us take a byte value.
byte val1 = (byte)90;
Before using the method, let us do some more manipulations. Mask the byte value now:
int res = val1 & 0xFF;
Let us now see the complete example and use the toHexString() method to convert a byte to hexadecimal equivalent.
Example
public class Demo { public static void main(String[] args) { byte val1 = (byte)90; System.out.println("Byte = "+val1); int res = val1 & 0xFF; System.out.println("Hexadecimal = "+Integer.toHexString(res)); } }
Output
Byte = 90 Hexadecimal = 5a
Advertisements