Java tutorial
//package com.java2s; /* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun * Microsystems, Inc. All Rights Reserved. */ import java.io.*; public class Main { /** * Decodes data encoded using {@link #toHex(byte[],int,int) toHex}. * * @param hex data to be converted * @param start offset * @param len count * * @throws IOException if input does not represent hex encoded value * * @since 1.29 */ public static byte[] fromHex(char[] hex, int start, int len) throws IOException { if (hex == null) throw new IOException("null"); int i = hex.length; if (i % 2 != 0) throw new IOException("odd length"); byte[] magic = new byte[i / 2]; for (; i > 0; i -= 2) { String g = new String(hex, i - 2, 2); try { magic[(i / 2) - 1] = (byte) Integer.parseInt(g, 16); } catch (NumberFormatException ex) { throw new IOException(ex.getLocalizedMessage()); } } return magic; } }