Java Hex Convert To convertHexStringToBinary(String hexString)

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

Description

Convert the provided hex-string into a binary representation where each byte represents two characters of the hex string.

License

Apache License

Parameter

Parameter Description
hexString string to convert

Exception

Parameter Description
IllegalArgumentException if the provided String is a non-even length or contains non-hex characters

Return

a byte array containing the binary representation

Declaration

public static byte[] convertHexStringToBinary(String hexString) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*from  w  ww. ja  v  a2  s.  co  m*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 the provided hex-string into a binary representation where each byte represents
     * two characters of the hex string.
     *
     * The hex characters may be upper or lower case.
     *
     * @param hexString string to convert
     * @return a byte array containing the binary representation
     * @throws IllegalArgumentException if the provided String is a non-even length or contains non-hex characters
     */
    public static byte[] convertHexStringToBinary(String hexString) throws IllegalArgumentException {
        int length = hexString.length();

        // As each byte needs two characters in the hex encoding, the string must be an even length.
        if (length % 2 != 0) {
            throw new IllegalArgumentException("The provided hex String must be an even length, but was of length "
                    + length + ": " + hexString);
        }

        byte[] binary = new byte[length / 2];

        for (int i = 0; i < length; i += 2) {
            char highBitsChar = hexString.charAt(i);
            char lowBitsChar = hexString.charAt(i + 1);

            int highBits = hexCharToInt(highBitsChar, hexString) << 4;
            int lowBits = hexCharToInt(lowBitsChar, hexString);

            binary[i / 2] = (byte) (highBits + lowBits);
        }

        return binary;
    }

    private static int hexCharToInt(char ch, String orig) throws IllegalArgumentException {
        if (ch >= '0' && ch <= '9') {
            // subtract '0' to get difference in position as an int
            return ch - '0';
        } else if (ch >= 'A' && ch <= 'F') {
            // subtract 'A' to get difference in position as an int
            // and then add 10 for the offset of 'A'
            return ch - 'A' + 10;
        } else if (ch >= 'a' && ch <= 'f') {
            // subtract 'a' to get difference in position as an int
            // and then add 10 for the offset of 'a'
            return ch - 'a' + 10;
        }

        throw new IllegalArgumentException(
                "The provided hex string contains non-hex character '" + ch + "': " + orig);
    }
}

Related

  1. convertHexByteToTelcoChar(byte byteValue)
  2. convertHexChars(final String value)
  3. convertHexColorToRgb(final String hex)
  4. convertHexFloatingPointLiteralToBits(char[] source)
  5. convertHexLong(String hex)
  6. convertHexTime2Binary(String timespan)
  7. convertHextoASCII(String text)
  8. convertHexToChar(String hex)
  9. convertHexToFloat(long hexValue)