Bytes to hex string


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public String bytesToHexString(byte[] b)
  2. {
  3. final char [] chars = new char[]
  4. {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F'};
  5.  
  6. if ((b == null) || (b.length == 0)) {
  7. throw new IllegalArgumentException("argument null or zero length");
  8. }
  9. StringBuffer buff = new StringBuffer(b.length * 2);
  10.  
  11. for (int i = 0; i < b.length; i++ ) {
  12. buff.insert(i*2,chars[(b[i] >> 4) & 0xf]);
  13. buff.insert(i*2+1,chars[b[i] & 0xf]);
  14. }
  15.  
  16. return buff.toString();
  17. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.