Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. public static String decToHex(int dec) {
  2.     int sizeOfIntInHalfBytes = 8;
  3.     int numberOfBitsInAHalfByte = 4;
  4.     int halfByte = 0x0F;
  5.     char[] hexDigits = {
  6.         '0', '1', '2', '3', '4', '5', '6', '7',
  7.         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  8.       };
  9.     StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
  10.     hexBuilder.setLength(sizeOfIntInHalfBytes);
  11.     for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
  12.     {
  13.       int j = dec & halfByte;
  14.       hexBuilder.setCharAt(i, hexDigits[j]);
  15.       dec >>= numberOfBitsInAHalfByte;
  16.     }
  17.     return hexBuilder.toString();
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement