Solved

Convert String To ASCII Code.

Posted on 2000-02-28
6
20,591 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now