List of usage examples for java.lang Character forDigit
public static char forDigit(int digit, int radix)
From source file:Main.java
private static String intToHex2(final int i) { final char hex_2[] = { Character.forDigit((i >> 4) & 0x0f, 16), Character.forDigit(i & 0x0f, 16) }; return new String(hex_2).toUpperCase(Locale.ENGLISH); }
From source file:Main.java
private static String toHexEncode(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { int low = bytes[i] & 0xF; int high = (bytes[i] >>> 4) & 0xF; sb.append(Character.forDigit(high, 16)); sb.append(Character.forDigit(low, 16)); }//from www . j a v a 2 s . c o m return sb.toString(); }
From source file:Main.java
public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; }//from w w w. j av a 2s. c om char[] buffer = new char[2]; for (int i = 0; i < src.length; i++) { buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16); buffer[1] = Character.forDigit(src[i] & 0x0F, 16); stringBuilder.append(buffer); } return stringBuilder.toString(); }
From source file:Main.java
private static String toHex(byte[] results) { if (results == null) return null; StringBuilder hexString = new StringBuilder(); for (int i = 0; i < results.length; i++) { int hi = (results[i] >> 4) & 0x0f; int lo = results[i] & 0x0f; hexString.append(Character.forDigit(hi, 16)).append(Character.forDigit(lo, 16)); }//from w ww. jav a 2s . co m return hexString.toString(); }
From source file:Main.java
public static void encodeHexString(byte[] bytes, StringBuilder buffer) { for (byte b : bytes) { int hi = (b >>> 4) & 0x0f; int lo = (b >>> 0) & 0x0f; buffer.append(Character.forDigit(hi, 16)); buffer.append(Character.forDigit(lo, 16)); }/*from w w w. j a va2 s . c om*/ }
From source file:Main.java
/** * encode Unicode string/*from www . j a v a2s .co m*/ * * @param s s * @return string */ public static String encodeUnicodeStr(String s) { StringBuilder sb = new StringBuilder(s.length() * 3); for (char c : s.toCharArray()) { if (c < 256) { sb.append(c); } else { sb.append("\\u"); sb.append(Character.forDigit((c >>> 12) & 0xf, 16)); sb.append(Character.forDigit((c >>> 8) & 0xf, 16)); sb.append(Character.forDigit((c >>> 4) & 0xf, 16)); sb.append(Character.forDigit((c) & 0xf, 16)); } } return sb.toString(); }
From source file:Main.java
public static String bytesToHexString(byte[] paramArrayOfByte) { StringBuilder localStringBuilder = new StringBuilder(); if ((paramArrayOfByte == null) || (paramArrayOfByte.length <= 0)) { return null; }// w ww. j a va 2s.c om char[] arrayOfChar = new char[2]; for (int i = 0;; i++) { if (i >= paramArrayOfByte.length) { return localStringBuilder.toString().toUpperCase(); } arrayOfChar[0] = Character.forDigit(0xF & paramArrayOfByte[i] >>> 4, 16); arrayOfChar[1] = Character.forDigit(0xF & paramArrayOfByte[i], 16); localStringBuilder.append(arrayOfChar); } }
From source file:Main.java
private static String md5(String in) { MessageDigest digest;/*w w w.j ava 2 s. c o m*/ try { digest = MessageDigest.getInstance("MD5"); digest.reset(); digest.update(in.getBytes()); byte[] a = digest.digest(); int len = a.length; StringBuilder sb = new StringBuilder(len << 1); for (int i = 0; i < len; i++) { sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16)); sb.append(Character.forDigit(a[i] & 0x0f, 16)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String md5(String s) { try {//w w w . ja v a2 s.c o m MessageDigest digester = MessageDigest.getInstance("MD5"); digester.update(s.getBytes("UTF-8")); byte[] a = digester.digest(); int len = a.length; StringBuilder sb = new StringBuilder(len << 1); for (int i = 0; i < len; i++) { sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16)); sb.append(Character.forDigit(a[i] & 0x0f, 16)); } return sb.toString(); } catch (UnsupportedEncodingException e) { return ""; } catch (NoSuchAlgorithmException e) { return ""; } }
From source file:Main.java
public static String md5(final String in) { MessageDigest digest;//w ww . j av a2 s . c o m try { digest = MessageDigest.getInstance("MD5"); digest.reset(); digest.update(in.getBytes()); final byte[] a = digest.digest(); final int len = a.length; final StringBuilder sb = new StringBuilder(len << 1); for (int i = 0; i < len; i++) { sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16)); sb.append(Character.forDigit(a[i] & 0x0f, 16)); } return sb.toString(); } catch (final NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }