----- Original Message -----
From: "Sushil Singh" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 21, 2001 7:15 AM
Subject: 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
>
 Hi there.
I do not know of any class or method that will do that for you
automatically. I have included a small example that will do that for you. I
think it is quite optimized for speed, but I bet it could be more improved.

  try {
   String hexStr = "546F6F6C73206F6620746865205472616465"; //your original
string
   int hexLength = hexStr.length();
   char[] converted = new char[hexLength/2];
   for(int i=2; i-1<hexLength; i+=2) {
    converted[i/2-1]=(char)Integer.parseInt(hexStr.substring(i-2, i),16);
   }
   String convStr = new String(converted); //your converted string
  } catch (Exception e) {
   System.out.println("error in hex-to-string conversion.");
  }


try this, and good luck.

PS. If someone out there sees a bug in that code-snippet, or have a
suggestion that could improve performance, send me a mail.

Best regards Frank Karlstr�m
Web-developer

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to