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

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

Description

hex String To Bytes

Declaration

public static byte[] hexStringToBytes(String hexString) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }/*from  w w  w .  j  a  v  a2s .co  m*/
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}

Related

  1. stringToHexByte(String str)
  2. hexStringToBytes(String hexString)
  3. hexStr2Bytes(String src)
  4. hexStringToBytes(String hex)
  5. getBytes(String hexString)
  6. stringToHex(String ids)
  7. hexStringToBytes(String s)
  8. hexStringToBytes(String hexString, int offset, int count)
  9. fromHex(String dataString)