Android String to Hex Byte Array Convert hexStringToBytes(String hexString, int offset, int count)

Here you can find the source of hexStringToBytes(String hexString, int offset, int count)

Description

hex String To Bytes

License

Open Source License

Parameter

Parameter Description
hexString source string (with Hex representation)
offset starting offset
count the length

Return

byte array

Declaration

public static byte[] hexStringToBytes(String hexString, int offset,
        int count) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  w  w  .  j  av a 2  s .  c o  m*/
     * @param s source string (with Hex representation)
     * @return byte array
     */
    public static byte[] hexStringToBytes(String s) {
        if (null == s)
            return null;

        return hexStringToBytes(s, 0, s.length());
    }

    /**
     * @param   hexString   source string (with Hex representation)
     * @param   offset      starting offset
     * @param   count       the length
     * @return  byte array
     */
    public static byte[] hexStringToBytes(String hexString, int offset,
            int count) {
        if (null == hexString || offset < 0 || count < 2
                || (offset + count) > hexString.length())
            return null;

        byte[] buffer = new byte[count >> 1];
        int stringLength = offset + count;
        int byteIndex = 0;
        for (int i = offset; i < stringLength; i++) {
            char ch = hexString.charAt(i);
            if (ch == ' ')
                continue;
            byte hex = isHexChar(ch);
            if (hex < 0)
                return null;
            int shift = (byteIndex % 2 == 1) ? 0 : 4;
            buffer[byteIndex >> 1] |= hex << shift;
            byteIndex++;
        }
        byteIndex = byteIndex >> 1;
        if (byteIndex > 0) {
            if (byteIndex < buffer.length) {
                byte[] newBuff = new byte[byteIndex];
                System.arraycopy(buffer, 0, newBuff, 0, byteIndex);
                buffer = null;
                return newBuff;
            }
        } else {
            buffer = null;
        }
        return buffer;
    }

    /**
     * Return true if the string is HexChars(1234567890abcdefABCDEF).
     *
     */
    public static byte isHexChar(char ch) {
        if ('a' <= ch && ch <= 'f')
            return (byte) (ch - 'a' + 10);
        if ('A' <= ch && ch <= 'F')
            return (byte) (ch - 'A' + 10);
        if ('0' <= ch && ch <= '9')
            return (byte) (ch - '0');

        return -1;
    }

    /**
     * Method Check String 
     *
     * @param The string to be format.
     * 
     * @param checkSpaceFlag=false: skip the space.
     *
     */
    public static boolean isHexChar(String hexString, boolean checkSpaceFlag) {
        if (null == hexString || 0 == hexString.length())
            return false;

        int hexLen = hexString.length();
        int hexCharCount = 0;
        char ch;
        for (int i = 0; i < hexLen; i++) {
            ch = hexString.charAt(i);
            if (ch == ' ') {
                if (checkSpaceFlag)
                    return false;
            } else {
                if (isHexChar(ch) < 0)
                    return false;
                else
                    hexCharCount++;
            }
        }

        if (hexCharCount % 2 != 0)
            return false;

        return true;
    }

    /**
     * Method Check String 
     *
     * @param The string to be format.
     *
     */
    public static boolean isHexChar(String hexString) {
        return isHexChar(hexString, true);
    }
}

Related

  1. hexStringToBytes(String hex)
  2. getBytes(String hexString)
  3. hexStringToBytes(String hexString)
  4. stringToHex(String ids)
  5. hexStringToBytes(String s)
  6. fromHex(String dataString)
  7. fromHexString(String s)
  8. hexDecode(String data)