Here you can find the source of fromHexString(String str)
public static byte[] fromHexString(String str)
//package com.java2s; /*//from ww w .j av a2 s .c o m * Copyright 2001-2006 The Apache Software Foundation * * 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. */ public class Main { public static byte[] fromHexString(String str) { str = cleanHexString(str); int stringLength = str.length(); if ((stringLength & 0x1) != 0) { throw new IllegalArgumentException( "fromHexString requires an even number of hex characters"); } byte[] b = new byte[stringLength / 2]; for (int i = 0, j = 0; i < stringLength; i += 2, j++) { int high = convertChar(str.charAt(i)); int low = convertChar(str.charAt(i + 1)); b[j] = (byte) ((high << 4) | low); } return b; } public static String cleanHexString(String str) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) != 32 && str.charAt(i) != ':') { buf.append(str.charAt(i)); } } return buf.toString(); } public static int convertChar(char c) { if ('0' <= c && c <= '9') { return c - '0'; } else if ('a' <= c && c <= 'f') { return c - 'a' + 0xa; } else if ('A' <= c && c <= 'F') { return c - 'A' + 0xa; } else { throw new IllegalArgumentException("Invalid hex character: [" + c + "]"); } } }