How to convert byte array to hex string
-
Hi All, I got a piece of code from google to convert byte array to hex string...i din't understand few lines of code can any on ehelp me to understand ?? :)
public static String CellKeysGeneration() { byte[] btba = new byte[5]; Random r = new Random(); r.nextBytes(btba); for (int i = 0; i < btba.length; i++) { btba[i] = btba[i]; } String str = tohexString(btba); return str; } public static String tohexString(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { sb.append(tohex(bytes[i] >> 4)); sb.append(tohex(bytes[i])); } return sb.toString(); } public static char tohex(int nibble) { final char[] hexdigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E' , 'F' }; return hexdigit[nibble & 0xF]; }
In above three functions, 1) I dint understand why in toHex function we are always performing "and" operartion with 0xF. 2) I dint understand why in tohexString function we are performing ">>" operation with 4. Can any one help me to understand toHexString and toHex functions ?? :) Thanks in advance ! -
Hi All, I got a piece of code from google to convert byte array to hex string...i din't understand few lines of code can any on ehelp me to understand ?? :)
public static String CellKeysGeneration() { byte[] btba = new byte[5]; Random r = new Random(); r.nextBytes(btba); for (int i = 0; i < btba.length; i++) { btba[i] = btba[i]; } String str = tohexString(btba); return str; } public static String tohexString(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { sb.append(tohex(bytes[i] >> 4)); sb.append(tohex(bytes[i])); } return sb.toString(); } public static char tohex(int nibble) { final char[] hexdigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E' , 'F' }; return hexdigit[nibble & 0xF]; }
In above three functions, 1) I dint understand why in toHex function we are always performing "and" operartion with 0xF. 2) I dint understand why in tohexString function we are performing ">>" operation with 4. Can any one help me to understand toHexString and toHex functions ?? :) Thanks in advance ! -
Why have you reposted this question here? I already gave you a full explanation at While converting byte array to hexstring[^].
One of these days I'm going to think of a really clever signature.
Hey im new to this forum stuff... well i could not locate the question i posted in the discussions.So i just re-posted it.. hope mistakes are accepted for the first time :-D by the way thanks for the reply.
-
Hey im new to this forum stuff... well i could not locate the question i posted in the discussions.So i just re-posted it.. hope mistakes are accepted for the first time :-D by the way thanks for the reply.
IICTECH wrote:
i could not locate the question i posted
Hover your mouse over your name at the top right of the page and you will see links to your questions, answers, comments and forum messages. If you are new to the site then spend some time looking around to get familiar with it.
One of these days I'm going to think of a really clever signature.