Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Converting an ASCII character

Posted on 2003-03-16
15
Medium Priority
?
294 Views
Last Modified: 2010-03-31
Hi, im currently trying to get a sequence of ASCII numbers from an InputStream, I can get them out ok but I was wondering how to transform them into the intended character (i.e 50 48 48 is changed to 200).  Also I would like to know how to do the opposite of this and convert a string into the corresponding ASCII numbers.

Cheers,

Dan
0
Comment
Question by:DanJW
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8147513
char c = (int)0x50 ;

int i = (int)'a' ;

0
 

Author Comment

by:DanJW
ID: 8147631
I think I get what you were trying to say (I presume that the cast in the first expression was meant to be to char rather than int) but I was wondering if there was a way that you could do a sequence of ASCII numbers to a string and vice-versa.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8147722
Errr...if they are in a byte array, or a char array, then you can do:

String s = new String( byteArray ) ;

Hehe..oops...sorry about the (int) thing too :-/
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!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8147882
To convert a String into a sequence of numbers, do the following:

String s = "hello world";
char[] numberCodes = s.toCharArray();

chars are numbers, but interpreted as characters, so to print them out as numbers, do

for(int i = 0;i < numberCodes.length;i++) {
  System.out.println((int(numberCodes[i]);
}
0
 

Author Comment

by:DanJW
ID: 8148039
I think I get what you were trying to say (I presume that the cast in the first expression was meant to be to char rather than int) but I was wondering if there was a way that you could do a sequence of ASCII numbers to a string and vice-versa.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8149554
String has a constructor:

String ( char chararray[] ) ;

So if you have the ASCII values stored in a char array (say a[]), you can convert it to a string as:

String str = new String ( a ) ;

Sameway, you can obtain the characters of a string and print their integer values as CEHJ has already explained through his code.

Remember, the characters are always stored as numbers  (their equivalent integer values). It depends upon you whether you want to display it as a character or as a number. You can use type-casting for your purpose.

Mayank.

0
 
LVL 2

Accepted Solution

by:
functionpointer earned 160 total points
ID: 8149627
Isn't this what the Reader classes are for?

Remember-> There is no such thing as an ASCII number. Only ASCII characters. ;)

char[] chars = new char[128];
InputStreamReader isr = InputStreamReader( is );
isr.read( chars, 0, 128 );
String s = new String( chars );


Reverse:
// get some OutputStream os;
PrintWriter out = new PrintWriter( os );
out.print( s ); // writing ASCII
0
 

Author Comment

by:DanJW
ID: 8300081
Erm, a bit old but I forgot.  I did use InputStreamReader etc. in the end.  Thanks.

Dan
0
 

Expert Comment

by:CleanupPing
ID: 9058660
DanJW:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9726594
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ - refund points

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9726839
I would suggest functionpointer gets the points :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9726856
Yep
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9726891
Apologies to functionpointer.  I mis-interpretted the last comment from DanJW.

TimYates and CEHJ - Thank you ;-)

Points to functionpointer.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9726943
:-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

578 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