Android Hex String Create hexToBytes(CharSequence str)

Here you can find the source of hexToBytes(CharSequence str)

Description

Convert a string of hex digits to a byte array, with the first byte in the array being the MSB.

License

Apache License

Declaration

public static byte[] hexToBytes(CharSequence str) 

Method Source Code

//package com.java2s;
/**/*w w  w . jav  a 2  s .  c  o m*/
 * Copyright (c) 2000, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Convert a string of hex digits to a byte array, with the first
     * byte in the array being the MSB. The string passed in should be
     * just the raw digits (upper or lower case), with no leading
     * or trailing characters (like '0x' or 'h').
     * An odd number of characters is supported.
     * If the string is empty, an empty array will be returned.
     *
     * This is significantly faster than using
     *   new BigInteger(str, 16).toByteArray();
     * especially with larger strings. Here are the results of some
     * microbenchmarks done on a P4 2.8GHz 2GB RAM running
     * linux 2.4.22-gg11 and JDK 1.5 with an optimized build:
     *
     * String length        hexToBytes (usec)   BigInteger
     * -----------------------------------------------------
     * 16                       0.570                 1.43
     * 256                      8.21                 44.4
     * 1024                    32.8                 526
     * 16384                  546                121000
     */
    public static byte[] hexToBytes(CharSequence str) {
        byte[] bytes = new byte[(str.length() + 1) / 2];
        if (str.length() == 0) {
            return bytes;
        }
        bytes[0] = 0;
        int nibbleIdx = (str.length() % 2);
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!isHex(c)) {
                throw new IllegalArgumentException(
                        "string contains non-hex chars");
            }
            if ((nibbleIdx % 2) == 0) {
                bytes[nibbleIdx >> 1] = (byte) (hexValue(c) << 4);
            } else {
                bytes[nibbleIdx >> 1] += (byte) hexValue(c);
            }
            nibbleIdx++;
        }
        return bytes;
    }

    private static boolean isHex(char c) {
        return ((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f'))
                || ((c >= 'A') && (c <= 'F'));
    }

    private static int hexValue(char c) {
        if ((c >= '0') && (c <= '9')) {
            return (c - '0');
        } else if ((c >= 'a') && (c <= 'f')) {
            return (c - 'a') + 10;
        } else {
            return (c - 'A') + 10;
        }
    }
}

Related

  1. bytesToHexString(byte[] bArray)
  2. makeHex(int val)
  3. makeInt(String hex)
  4. appendHexJavaScriptRepresentation(int codePoint, Appendable out)
  5. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  6. hexValue(char c)
  7. isHex(char c)
  8. getHexString(int[] b)
  9. getHexString(int[] b, String splitString)