Because Character Objects and substring are about 100x slower than char
array manipulations.
(*Chris*)
----- Original Message -----
From: "Arnab Nandi" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 21, 2001 1:03 PM
Subject: Re: [JSP-INTEREST] Hexadecimal to String
Why write so many lines if such a small code will suffice?
Is there something wrong with this code?
public class HexToString{
public static void main(String[] args){
StringBuffer s = new
StringBuffer("546F6F6C73206F6620746865205472616465");
String sHexToString = new String("");
for(int i = 0; i < s.length(); i += 2){
String sTemp = s.substring(i, i + 2);
sHexToString += new Character((char)(Integer.valueOf(sTemp,
16).intValue())).toString();
}
System.out.println(sHexToString);
}
}
-----Original Message-----
From: Chris Pratt [mailto:[EMAIL PROTECTED]]
Sent: Thursday, June 21, 2001 9:35 PM
To: [EMAIL PROTECTED]
Subject: Re: Hexadecimal to String
Try something like this:
(*Chris*)
/**
* Junk
*
* $ID$
*
* @author Chris Pratt
* @version $Revision$
*
* 6/21/01
*/
public class Junk {
/**
* 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 convert (String str) {
if(str != null) {
char[] in = str.toCharArray();
StringBuffer buf = new StringBuffer(in.length / 2);
int i = 0;
while(i < in.length) {
buf.append((char)((ord(in[i++]) << 4) | ord(in[i++])));
}
return buf.toString();
}
return null;
} //convert
/**
* Program Entry-point
*
* @param args Command Line Arguments
*/
public static void main (String[] args) {
System.out.println(convert(args[0]));
} //main
} //*Junk
----- Original Message -----
From: "Sushil Singh" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, June 20, 2001 10:16 PM
Subject: [JSP-INTEREST] Hexadecimal to String
> Hi,
>
> Is it possible to convert hexadecimal value into String in java, is
> there any class/funcion is available?
>
> For ex:
> 546F6F6C73206F6620746865205472616465
> is equivalent to "Tools of the Trade"
>
> Thanks in advance.
>
> Sushil
>
>
========================================================================
===
> 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
_________________________________________________________
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
=========================
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=P
http://www.jguru.com/jguru/faq/faqpage.jsp?name=rvlets
_________________________________________________________
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