Android Hex String to Byte Array Convert fromHex(String hexData)

Here you can find the source of fromHex(String hexData)

Description

Converts a Hex-encoded data string to the original byte data.

License

Open Source License

Parameter

Parameter Description
hexData hex-encoded data to decode.

Return

decoded data from the hex string.

Declaration

public static byte[] fromHex(String hexData) 

Method Source Code

//package com.java2s;
/*//from w  ww  .ja  v  a2  s  .  c  om
 * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
 * for applicable license terms and NOTICE.txt for applicable notices.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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 {
    /**
     * Converts a Hex-encoded data string to the original byte data.
     * 
     * @param hexData
     *            hex-encoded data to decode.
     * @return decoded data from the hex string.
     */
    public static byte[] fromHex(String hexData) {
        byte[] result = new byte[(hexData.length() + 1) / 2];
        String hexNumber = null;
        int stringOffset = 0;
        int byteOffset = 0;
        while (stringOffset < hexData.length()) {
            hexNumber = hexData.substring(stringOffset, stringOffset + 2);
            stringOffset += 2;
            result[byteOffset++] = (byte) Integer.parseInt(hexNumber, 16);
        }
        return result;
    }
}

Related

  1. fromHex(String hex)
  2. hexStringToByteArray(String s)
  3. hexStringToBytes(String hex)
  4. hexStringToBytes(String hex)
  5. hexStringToBytes(String hexString)