Solved

Convert String To ASCII Code.

Posted on 2000-02-28
6
20,683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 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