Java XML Hex toByteArrayFromHexString(String s)

Here you can find the source of toByteArrayFromHexString(String s)

Description

Returns a byte array from a given hex string.

License

Open Source License

Parameter

Parameter Description
hexString The hexadecimal string representing a byte array. The lexical value space defined in XML Schema Part 2: Datatypes for xsd:hexBinary is expected: "hexBinary has a lexical representation where each binary octet is encoded as a character tuple, consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code. For example, '0FB7' is a hex encoding for the 16-bit integer 4023 (whose binary representation is 111110110111)."

Return

A byte array representing the hexadecimal string

Declaration

public static byte[] toByteArrayFromHexString(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2016 Dr.-Ing. Marc M?ltin.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:/*from   ww w  .j  a  v a  2  s.  c  o m*/
 *    Dr.-Ing. Marc M?ltin - initial API and implementation and initial documentation
 *******************************************************************************/

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**
     * Returns a byte array from a given hex string.
     * 
     * @param  hexString The hexadecimal string representing a byte array. The lexical value space 
     *                    defined in XML Schema Part 2: Datatypes for xsd:hexBinary is expected: 
     *                 "hexBinary has a lexical representation where each binary octet is encoded 
     *                 as a character tuple, consisting of two hexadecimal digits ([0-9a-fA-F]) 
     *                 representing the octet code. For example, '0FB7' is a hex encoding for the 
     *                 16-bit integer 4023 (whose binary representation is 111110110111)."
     * @return A byte array representing the hexadecimal string
     */
    public static byte[] toByteArrayFromHexString(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }
}

Related

  1. sha1Hex(String data)
  2. sha1Hex(String message)
  3. stringToHex(String str)
  4. toByteArray(String hex)
  5. toByteArray(String hexDumpString)
  6. toByteFromHexString(String hexString)
  7. toHex(byte[] bytes)
  8. toHex(byte[] bytes)
  9. toHex(byte[] donnees)