CHexConvert { public static byte[] hexStr2Bytes(String src){ src = src.trim().replace(" ", "").toUpperCase(Locale.US); int m=0,n=0; int iLen=src.length()/2; byte[] ret = new byte[iLen]; for (int i = 0; i < iLen; i++){ m=i*2+1; n=m+1; ret[i] = (byte)(Integer.decode("0x"+ src.substring(i*2, m) + src.substring(m,n)) & 0xFF); } return ret; }  static byte[] hexStr2Bytes(String src);  }
@Test public void testCHextConvert(){ byte[] output = CHexConvert.hexStr2Bytes("3132333435363738393031323334353637383931"); byte[] expect = {49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 49}; assertEquals(expect.length, output.length); for(int i=0; i<expect.length; i++) assertEquals(expect[i], output[i]); }