Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert String To ASCII Code.

Posted on 2000-02-28
6
Medium Priority
?
20,713 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 60 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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 …
Suggested Courses

596 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