Here you can find the source of toByteArray(CharSequence hexString)
Parameter | Description |
---|---|
hexString | The String, StringBuilder, etc. that contains the sequence of hexidecimal character values. |
public static byte[] toByteArray(CharSequence hexString)
//package com.java2s; /*/*from w w w. j a v a 2s. co m*/ * #%L * mfz-util * %% * Copyright (C) 2012 mfizz * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License 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. * #L% */ public class Main { /** * Creates a byte array from a CharSequence (String, StringBuilder, etc.) * containing only valid hexidecimal formatted characters. * Each grouping of 2 characters represent a byte in "Big Endian" format. * The hex CharSequence must be an even length of characters. For example, a String * of "1234" would return the byte array { 0x12, 0x34 }. * @param hexString The String, StringBuilder, etc. that contains the * sequence of hexidecimal character values. * @return A new byte array representing the sequence of bytes created from * the sequence of hexidecimal characters. If the hexString is null, * then this method will return null. */ public static byte[] toByteArray(CharSequence hexString) { if (hexString == null) { return null; } return toByteArray(hexString, 0, hexString.length()); } /** * Creates a byte array from a CharSequence (String, StringBuilder, etc.) * containing only valid hexidecimal formatted characters. * Each grouping of 2 characters represent a byte in "Big Endian" format. * The hex CharSequence must be an even length of characters. For example, a String * of "1234" would return the byte array { 0x12, 0x34 }. * @param hexString The String, StringBuilder, etc. that contains the * sequence of hexidecimal character values. * @param offset The offset within the sequence to start from. If the offset * is invalid, will cause an IllegalArgumentException. * @param length The length from the offset to convert. If the length * is invalid, will cause an IllegalArgumentException. * @return A new byte array representing the sequence of bytes created from * the sequence of hexidecimal characters. If the hexString is null, * then this method will return null. */ public static byte[] toByteArray(CharSequence hexString, int offset, int length) { if (hexString == null) { return null; } assertOffsetLengthValid(offset, length, hexString.length()); // a hex string must be in increments of 2 if ((length % 2) != 0) { throw new IllegalArgumentException( "The hex string did not contain an even number of characters [actual=" + length + "]"); } // convert hex string to byte array byte[] bytes = new byte[length / 2]; int j = 0; int end = offset + length; for (int i = offset; i < end; i += 2) { int highNibble = hexCharToIntValue(hexString.charAt(i)); int lowNibble = hexCharToIntValue(hexString.charAt(i + 1)); bytes[j++] = (byte) (((highNibble << 4) & 0xF0) | (lowNibble & 0x0F)); } return bytes; } static private void assertOffsetLengthValid(int offset, int length, int arrayLength) { if (offset < 0) { throw new IllegalArgumentException("The array offset was negative"); } if (length < 0) { throw new IllegalArgumentException("The array length was negative"); } if (offset + length > arrayLength) { throw new ArrayIndexOutOfBoundsException("The array offset+length would access past end of array"); } } /** * Converts a hexidecimal character such as '0' or 'A' or 'a' to its integer * value such as 0 or 10. Used to decode hexidecimal Strings to integer values. * @param c The hexidecimal character * @return The integer value the character represents * @throws IllegalArgumentException Thrown if a character that does not * represent a hexidecimal character is used. */ static public int hexCharToIntValue(char c) { // always faster to do case statements switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'A': case 'a': return 10; case 'B': case 'b': return 11; case 'C': case 'c': return 12; case 'D': case 'd': return 13; case 'E': case 'e': return 14; case 'F': case 'f': return 15; default: throw new IllegalArgumentException("The character [" + c + "] does not represent a valid hex digit"); } } }