Android String to Hex Byte Array Convert hexStringToBytes(String hex)

Here you can find the source of hexStringToBytes(String hex)

Description

hex String To Bytes

Declaration

public static byte[] hexStringToBytes(String hex) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] hexStringToBytes(String hex) {
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            byte b1 = (byte) "0123456789ABCDEF".indexOf(achar[pos]);
            byte b2 = (byte) "0123456789ABCDEF".indexOf(achar[(pos + 1)]);
            result[i] = (byte) (b1 << 4 | b2);
        }//from  ww  w  .  j a va 2s .  com
        return result;
    }
}

Related

  1. stringToHexByte(String str)
  2. hexStringToBytes(String hexString)
  3. hexStr2Bytes(String src)
  4. getBytes(String hexString)
  5. hexStringToBytes(String hexString)
  6. stringToHex(String ids)
  7. hexStringToBytes(String s)