Description
Converts the hex string to a byte array.
License
Open Source License
Parameter
Parameter | Description |
---|
hexString | the string to decode. Must have an even number of characters, each pair of which represents a hexadecimal byte. |
Exception
Parameter | Description |
---|
NumberFormatException | if the string can not be parsed as asequence of hexadecimal digits for any reason. |
Return
a numerical representation of the input string.
Declaration
public static byte[] fromHexString(String hexString) throws NumberFormatException
Method Source Code
//package com.java2s;
/*// ww w .j a v a 2 s . c o m
* TextUtilities.java (Class: com.madphysicist.tools.util.TextUtilities)
*
* Mad Physicist JTools Project (General Purpose Utilities)
*
* The MIT License (MIT)
*
* Copyright (c) 2012 by Joseph Fox-Rabinovitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public class Main {
/**
* Converts the hex string to a byte array. The string must have an even
* number of digits, each of which must be a valid hexadecimal digit. Every
* two digits represent a successive byte. This method is an almost exact
* inverse of {@link #toHexString(byte[])}. Calling {@code
* toHexString(fromHexString(hexString))} will return the original string,
* up to the case of the hexadecimal digits.
*
* @param hexString the string to decode. Must have an even number of
* characters, each pair of which represents a hexadecimal byte.
* @return a numerical representation of the input string.
* @throws NumberFormatException if the string can not be parsed as a
* sequence of hexadecimal digits for any reason.
* @since 1.0.0
*/
public static byte[] fromHexString(String hexString) throws NumberFormatException {
if (hexString.length() % 2 != 0) {
throw new NumberFormatException("odd number of digits: " + hexString);
}
byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < bytes.length; i++) {
char d1 = hexString.charAt(i * 2);
char d2 = hexString.charAt(i * 2 + 1);
if (Character.digit(d1, 16) < 0 || Character.digit(d2, 16) < 0) {
throw new NumberFormatException("not a hex string" + hexString);
}
bytes[i] = (byte) (Integer.parseInt("" + d1 + d2, 16) & 0xFF);
}
return bytes;
}
}
Related
- fromHexString(String encoded)
- fromHexString(String encoded)
- fromHexString(String hex)
- fromHexString(String hex)
- fromHexString(String hexString)
- fromHexString(String hexString)
- fromHexString(String hexString)
- fromHexString(String in)
- fromHexString(String input)