It's a little more complicated than that. Here's the complete code:
(*Chris*)
/**
* Junk
*
* $ID$
*
* @author Chris Pratt
* @version $Revision$
*
* 6/21/01
*/
public class Junk {
private static final char[] DIGITS =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* Return the character for the ordinal number provided
*
* @param n a single digit number
* @return A character
*/
public static char chr (int n) {
return DIGITS[n & 0xF];
} //chr
/**
* Encode a String as Hexidecimal Characters
*
* @param str String to be Encoded
* @return String of Hexidecimal Characters
*/
public static String encode (String str) {
if(str != null) {
char[] in = str.toCharArray();
StringBuffer out = new StringBuffer(in.length * 2);
int i = 0;
while(i < in.length) {
out.append(chr(in[i] >> 4)).append(chr(in[i++]));
}
return out.toString();
}
return null;
} //encode
/**
* Return the ordinal number of the character provided
*
* @param c One of (0 - 9, A - F, a - f)
* @return An integer between 0 and 15
*/
public static int ord (char c) {
if(c <= '9') {
return ((int)c) - ((int)'0');
} else if(c <= 'F') {
return ((int)c) - ((int)'A') + 10;
} else {
return ((int)c) - ((int)'a') + 10;
}
} //ord
/**
* Convert a Hexidecimal String into a Character String
*
* @param str A String of Hexidecimal Characters
* @return A String
*/
public static String decode (String str) {
if(str != null) {
char[] in = str.toCharArray();
StringBuffer out = new StringBuffer(in.length / 2);
int i = 0;
while(i < in.length) {
out.append((char)((ord(in[i++]) << 4) | ord(in[i++])));
}
return out.toString();
}
return null;
} //decode
/**
* Program Entry-point
*
* @param args Command Line Arguments
*/
public static void main (String[] args) {
String str = encode(args[0]);
System.out.println(str);
System.out.println(decode(str));
} //main
} //*Junk
----- Original Message -----
From: "Sharon Rose Orillaneda" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, June 24, 2001 7:11 PM
Subject: Re: [JSP-INTEREST] Hexadecimal to String
> Hi Chris!
>
> This is actually decryption. Do you have an algo for encryption? Like
"Tools
> of the Trade" will be converted to "546F6F6C73206F6620746865205472616465"
?
> Am I correct if I will change the + 10 to -10 in order to get the
encrypted
> vaue? Sorry, I'm not really good on this Hexa stuff.
>
> Thanks.
>
> -Sharon
>
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets