/* HexUtil.java * * Copyright (c) 2001, Ted Nelson and Tuomas Lukka * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.util; /** Convert between byte arrays and strings of Hex digits */ public final class HexUtil { public static final String rcsid = "$Id: HexUtil.java,v 1.1 2003/03/25 13:57:38 tjl Exp $"; /** Generate a readable hex string representation of the given byte array. * If the array is long, show only the beginning, the part around * the index ind and the end (not yet implemented). */ static public String toString(byte[] b, int index) { if(b == null) return "null"; StringBuffer buf = new StringBuffer("{"); for(int i=0; i= '0' && ch <= '9') return ch - '0'; if(ch >= 'A' && ch <= 'F') return ch - 'A' + 10; if(ch >= 'a' && ch <= 'f') return ch - 'a' + 10; throw new NumberFormatException("Not a hexadecimal digit: "+((char)ch)); } static char character(int digit) { if(digit >= 0 && digit <= 9) return (char)(digit + '0'); else if(digit >= 10 && digit <= 15) return (char)(digit + 'A' - 10); throw new Error("DigitToChar!"); } static public byte[] hexToByteArr(String hex) throws NumberFormatException { if(hex.length() % 2 > 0) throw new NumberFormatException("Odd number of digits: "+hex); byte[] b = new byte[hex.length() / 2]; for(int i=0; i