Link to home
Start Free TrialLog in
Avatar of HuangAun
HuangAun

asked on

Convert String To ASCII Code.

Let said I have a string
String test="Test Code";
Can I convert the above string to ASCII code.  And then convert back the ASCII code to exact string. Is there any command in Java  that can convert it.  Thanks.
Huang Aun
ASKER CERTIFIED SOLUTION
Avatar of _lychee_
_lychee_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of terajiv
terajiv

if u write String
String test="Test Code";
like this U will get Unicode for perticular char. by using
getNumaricValue() method. from Character class.
I dont think its possible to get in terms of ASCII.
If u use String Constructors as lychee has shown u can get it.
Try this out :

....

String str = new String ("<YOUR STRING>") ;
String ascii_str ;

try {
    ascii_str = String (str.getBytes ("ASCII")) ;
} catch (UnsupportedEncodingException e) {
   System.out.println ("Unsupported Conversion") ;
   return (1) ;
}

System.out.println (ascii_str) ;
 ....
Avatar of HuangAun

ASKER

Hi All!
I tried already the above suggestion.  But didnt work.  Hope that can give more details solution.  Thanks,
Regards,
Huang Aun
post ur code pls...
Hi,

try this one. works fine in my PC.

----- Begin ----------

import java.io.* ;
import java.lang.* ;

class tapak
{
      public static void main (String args[])
      {
                   String str = new String ("<YOUR STRING>") ;
                   String ascii_str = null ;

                   try {

                       // my earlier statement was
                       // ascii_str = String (str.getBytes ("ASCII")) ;
                       ascii_str = new String (str.getBytes ("ASCII")) ;
                   }
                   catch (UnsupportedEncodingException e)
                   {
                      System.out.println ("Unsupported Conversion") ;
                   }

                   System.out.println ("ASCII version : " + ascii_str) ;

      }
}


---------- End -------------

Let me know if you have any doubts.