Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String decToHex(int dec) {
- int sizeOfIntInHalfBytes = 8;
- int numberOfBitsInAHalfByte = 4;
- int halfByte = 0x0F;
- char[] hexDigits = {
- '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
- };
- StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
- hexBuilder.setLength(sizeOfIntInHalfBytes);
- for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
- {
- int j = dec & halfByte;
- hexBuilder.setCharAt(i, hexDigits[j]);
- dec >>= numberOfBitsInAHalfByte;
- }
- return hexBuilder.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement