Java Hex Convert To fromHexString(final String hexaString)

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

Description

Creates a ByteBuffer whose content is represented as hexadecimal in the given String.

License

Open Source License

Parameter

Parameter Description
hexaString the hexadecimal string

Return

the created ByteBuffer

Declaration

public static byte[] fromHexString(final String hexaString) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.j a v  a 2s  . co  m*/
 *
 * JMeta - Meta's java implementation
 *
 * Copyright (C) 2013-2015 Pablo Joubert
 * Copyright (C) 2013-2015 Thomas Lavocat
 * Copyright (C) 2013-2015 Nicolas Michon
 *
 * This file is part of JMeta.
 *
 * JMeta is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * JMeta 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * Creates a ByteBuffer whose content is represented as hexadecimal in the given String.
     *
     * @param hexaString the hexadecimal string
     * @return the created ByteBuffer
     */
    public static byte[] fromHexString(final String hexaString) {
        if ((hexaString.length() % 2) != 0) {
            throw new IllegalArgumentException("Input string must contain an even number of characters");
        }
        int length = hexaString.length();
        byte[] buf = new byte[length / 2];

        for (int i = 0; i < length; i += 2) {
            buf[i / 2] = (byte) ((Character.digit(hexaString.charAt(i), 16) << 4)
                    + Character.digit(hexaString.charAt(i + 1), 16));
        }
        return buf;
    }
}

Related

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