Here you can find the source of fromHexString(String hexString)
Parameter | Description |
---|---|
hexString | Hex string, such as "0123456789ABCDEF", where each byte is represented by two characters |
public static byte[] fromHexString(String hexString)
//package com.java2s; /*/*from ww w. j a v a 2 s. c o m*/ * File: HashFunctionUtil.java * Authors: Kevin R. Dixon * Company: Sandia National Laboratories * Project: Cognitive Foundry * * Copyright Jan 26, 2011, Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the U.S. Government. * Export of this program may require a license from the United States * Government. See CopyrightHistory.txt for complete details. * */ public class Main { /** * Char table for convenience */ static final char[] HEX_CHAR_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Converts the hex-string back into an array of bytes * @param hexString * Hex string, such as "0123456789ABCDEF", where each byte is represented * by two characters * @return * Byte representation of the hex string, will be half the length of string */ public static byte[] fromHexString(String hexString) { // It takes two chars to represent a single byte final int sl = hexString.length(); if ((sl % 2) != 0) { throw new IllegalArgumentException("hexString must be even length"); } byte[] bytes = new byte[sl / 2]; for (int i = 0; i < sl; i += 2) { final int j = i / 2; byte high = valueOf(hexString.charAt(i)); byte low = valueOf(hexString.charAt(i + 1)); byte value = (byte) (((high & 0xf) << 4) | (low & 0xf)); bytes[j] = value; } return bytes; } /** * Returns the hex value of the given character * @param hexChar * Hex character, such as '0' or 'A' * @return * The value of the hex character. */ public static byte valueOf(char hexChar) { hexChar = Character.toLowerCase(hexChar); for (byte i = 0; i < HEX_CHAR_TABLE.length; i++) { if (hexChar == HEX_CHAR_TABLE[i]) { return i; } } throw new IllegalArgumentException(hexChar + " is not a hex char"); } }