Solved

Convert String To ASCII Code.

Posted on 2000-02-28
6
20,638 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:HuangAun
6 Comments
 
LVL 2

Accepted Solution

by:
_lychee_ earned 20 total points
ID: 2567653
byte[] b = s.getBytes("ASCII");
String test2 = new String(b, "ASCII");

b contains ASCII (if original string consists of only ascii of course...)

if your default encoding is ascii/iso____ (i can't remember) u don't need the "ASCII"
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2567722
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.
0
 
LVL 1

Expert Comment

by:raghurani
ID: 2568538
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) ;
 ....
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:HuangAun
ID: 2579349
Hi All!
I tried already the above suggestion.  But didnt work.  Hope that can give more details solution.  Thanks,
Regards,
Huang Aun
0
 
LVL 2

Expert Comment

by:_lychee_
ID: 2579673
post ur code pls...
0
 
LVL 1

Expert Comment

by:raghurani
ID: 2618676
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
hibernate insert example 13 34
How to execute a Python program and gather return output in Java 2 40
printf performancy 11 66
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question