This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Hexadecimal2String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Hexadecimal2String" Watch "Hexadecimal2String" New topic
Author

Hexadecimal2String

Andrew Obodoefuna
Greenhorn

Joined: Dec 03, 2001
Posts: 5
Hello,
Can anyone tell he how to convert a String in Hexadecimal format into a regular String?
Many Thanks,
Andrew.
Ken Goodhew
Greenhorn

Joined: Dec 07, 2001
Posts: 1
/*
* $Header:
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/HexUtils.java,v 1.3 2001/07/22 20:25:13 pier Exp $
* $Revision: 1.3 $
* $Date: 2001/07/22 20:25:13 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software
itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products
derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/

package com.arg.util;
import java.io.ByteArrayOutputStream;
/**
* Library of utility methods useful in dealing with converting byte
arrays
* to and from strings of hexadecimal digits.
*
* @author Craig R. McClanahan
*/
public final class HexUtils {
// Code from Ajp11, from Apache's JServ
// Table for HEX to DEC byte translation
public static final int[] DEC = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
/**
* Convert a String of hexadecimal digits into the corresponding
* byte array by encoding each two hexadecimal digits as a byte.
*
* @param digits Hexadecimal digits representation
*
* @exception IllegalArgumentException if an invalid hexadecimal
digit
* is found, or the input string contains an odd number of
hexadecimal
* digits
*/
public static byte[] convert(String digits) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i = 0; i < digits.length(); i += 2) {<br /> char c1 = digits.charAt(i);<br /> if ((i+1) >= digits.length())
throw new IllegalArgumentException
("hexUtil.odd");
char c2 = digits.charAt(i + 1);
byte b = 0;
if ((c1 >= '0') && (c1 <= '9'))<br /> b += ((c1 - '0') * 16);<br /> else if ((c1 >= 'a') && (c1 <= 'f'))<br /> b += ((c1 - 'a' + 10) * 16);<br /> else if ((c1 >= 'A') && (c1 <= 'F'))<br /> b += ((c1 - 'A' + 10) * 16);<br /> else<br /> throw new IllegalArgumentException<br /> ("hexUtil.bad");<br /> if ((c2 >= '0') && (c2 <= '9'))<br /> b += (c2 - '0');<br /> else if ((c2 >= 'a') && (c2 <= 'f'))<br /> b += (c2 - 'a' + 10);<br /> else if ((c2 >= 'A') && (c2 <= 'F'))<br /> b += (c2 - 'A' + 10);<br /> else<br /> throw new IllegalArgumentException<br /> ("hexUtil.bad");<br /> baos.write(b);<br /> }<br /> return (baos.toByteArray());<br /> }<br /> <br /> /**<br /> * Convert a byte array into a printable format containing a<br /> * String of hexadecimal digit characters (two per byte).<br /> *<br /> * @param bytes Byte array representation<br /> */<br /> public static String convert(byte bytes[]) {<br /> /*<br /> StringBuffer sb = new StringBuffer(bytes.length * 2);<br /> for (int i = 0; i < bytes.length; i++) {<br /> sb.append(convertDigit((int) (bytes[i] >> 4)));
sb.append(convertDigit((int) (bytes[i] & 0x0f)));
}
return (sb.toString());
*/
return convert(bytes, 0, bytes.length);
}
public static String convert(byte bytes[], int offset, int length) {
StringBuffer sb = new StringBuffer(bytes.length * 2);
for (int i = 0; i < length; i++) {<br /> sb.append(convertDigit((int) (bytes[offset + i] >> 4)));
sb.append(convertDigit((int) (bytes[offset + i] & 0x0f)));
}

return sb.toString().toUpperCase();
}

/**
* Convert 4 hex digits to an int, and return the number of
converted
* bytes.
*
* @param hex Byte array containing exactly four hexadecimal digits
*
* @exception IllegalArgumentException if an invalid hexadecimal
digit
* is included
*/
public static int convert2Int( byte[] hex ) {
// Code from Ajp11, from Apache's JServ
// assert b.length==4
// assert valid data
int len;
if(hex.length < 4 ) return 0;
if( DEC[hex[0]]<0 )
throw new IllegalArgumentException("hexUtil.bad");
len = DEC[hex[0]];
len = len << 4;
if( DEC[hex[1]]<0 )
throw new IllegalArgumentException("hexUtil.bad");
len += DEC[hex[1]];
len = len << 4;
if( DEC[hex[2]]<0 )
throw new IllegalArgumentException("hexUtil.bad");
len += DEC[hex[2]];
len = len << 4;
if( DEC[hex[3]]<0 )
throw new IllegalArgumentException("hexUtil.bad");
len += DEC[hex[3]];
return len;
}
/**
* [Private] Convert the specified value (0 .. 15) to the
corresponding
* hexadecimal digit.
*
* @param value Value to be converted
*/
private static char convertDigit(int value) {
value &= 0x0f;
if (value >= 10)
return ((char) (value - 10 + 'a'));
else
return ((char) (value + '0'));
}

}
Andrew Obodoefuna
Greenhorn

Joined: Dec 03, 2001
Posts: 5
Cheers Ken, thats really kool.
Andrew.
 
Don't get me started about those stupid light bulbs.
 
subject: Hexadecimal2String
 
Similar Threads
Tech Word Game
date format with MS Access database
ResultSet to Array
WA #1.....word association
How to convert byte array into string array?