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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this out :
....
String str = new String ("<YOUR STRING>") ;
String ascii_str ;
try {
ascii_str = String (str.getBytes ("ASCII")) ;
} catch (UnsupportedEncodingExcept ion e) {
System.out.println ("Unsupported Conversion") ;
return (1) ;
}
System.out.println (ascii_str) ;
....
....
String str = new String ("<YOUR STRING>") ;
String ascii_str ;
try {
ascii_str = String (str.getBytes ("ASCII")) ;
} catch (UnsupportedEncodingExcept
System.out.println ("Unsupported Conversion") ;
return (1) ;
}
System.out.println (ascii_str) ;
....
ASKER
Hi All!
I tried already the above suggestion. But didnt work. Hope that can give more details solution. Thanks,
Regards,
Huang Aun
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 (UnsupportedEncodingExcept ion e)
{
System.out.println ("Unsupported Conversion") ;
}
System.out.println ("ASCII version : " + ascii_str) ;
}
}
---------- End -------------
Let me know if you have any doubts.
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 (UnsupportedEncodingExcept
{
System.out.println ("Unsupported Conversion") ;
}
System.out.println ("ASCII version : " + ascii_str) ;
}
}
---------- End -------------
Let me know if you have any doubts.
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.