Java Hex Convert To fromHexString(final String hexString)

Here you can find the source of fromHexString(final String hexString)

Description

Converts the provided String of hexadecimal numbers (with optional leading "0x") into a byte[] of the same value.

License

Open Source License

Parameter

Parameter Description
hexString the input string containing a hexadecimal value.

Return

a byte[] of the same value, or null if hexString is null or contains no characters.

Declaration

public static byte[] fromHexString(final String hexString) 

Method Source Code

//package com.java2s;
/*/*w  w  w.j  av a  2s.  c om*/
 * Owl Platform Common Library for Java
 * Copyright (C) 2012 Robert Moore and the Owl Platform
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

public class Main {
    /**
     * Converts the provided String of hexadecimal numbers (with optional leading
     * "0x") into a {@code byte[]} of the same value. Invalid characters (outside
     * the range 0-F) will be given the value 0.
     * 
     * @param hexString
     *          the input string containing a hexadecimal value.
     * @return a {@code byte[]} of the same value, or {@code null} if
     *         {@code hexString} is {@code null} or contains no characters.
     */
    public static byte[] fromHexString(final String hexString) {
        if (hexString == null) {
            return null;
        }

        String str = hexString.trim().toLowerCase();

        // Strip any leading "0x"
        if (str.startsWith("0x")) {
            str = str.substring(2);
        }

        int numChars = str.length();
        if (numChars == 0) {
            return null;
        }
        char[] chars = str.toCharArray();
        byte[] retVal = new byte[(numChars + 1) / 2];
        int byteIndex = 0;
        int i = 0;
        if ((chars.length & 0x01) == 1) {
            retVal[byteIndex++] = fromChar(chars[i++]);
        }
        for (; i < chars.length; i += 2, ++byteIndex) {
            retVal[byteIndex] = (byte) ((fromChar(chars[i]) << 4) | fromChar(chars[i + 1]));
        }
        return retVal;
    }

    /**
     * Converts a single hexadecimal character into a byte. The value
     * of the character is stored in the lower 4 bits of the returned byte.
     * @param nibble the hexadecimal character to convert.
     * @return a byte containing the binary equivalent of the hexadecimal character.
     */
    private static byte fromChar(final char nibble) {
        if (nibble >= '0' && nibble <= '9') {
            return (byte) (nibble - '0');
        }
        if (nibble >= 'a' && nibble <= 'f') {
            return (byte) (nibble - 'a' + 10);
        }

        return 0;
    }
}

Related

  1. fromHexNibble(final char n)
  2. fromHexShort(char a)
  3. fromHexStr(final String data)
  4. fromHexString(byte abyte0[], int i)
  5. fromHexString(final String hexaString)
  6. fromHexString(final String s)
  7. fromHexString(final String str)
  8. fromHexString(String encoded)
  9. fromHexString(String encoded)