Android examples for java.lang:Hex
decode Hex Str to byte array
import android.util.Log; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; public class Main{ public static byte[] decodeHexStr(final String str) { if (str == null) { return null; }//from w w w . j av a 2s . co m char[] charArray = str.toCharArray(); if (charArray.length % 2 != 0) { throw new RuntimeException( "hex str length must can mod 2, str:" + str); } byte[] bytes = new byte[charArray.length / 2]; for (int i = 0; i < charArray.length; i++) { char c = charArray[i]; int b; if (c >= '0' && c <= '9') { b = (c - '0') << 4; } else if (c >= 'A' && c <= 'F') { b = (c - 'A' + 10) << 4; } else { throw new RuntimeException("unsport hex str:" + str); } c = charArray[++i]; if (c >= '0' && c <= '9') { b |= c - '0'; } else if (c >= 'A' && c <= 'F') { b |= c - 'A' + 10; } else { throw new RuntimeException("unsport hex str:" + str); } bytes[i / 2] = (byte) b; } return bytes; } }