?
Solved

Converting an ASCII character

Posted on 2003-03-16
15
Medium Priority
?
293 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
[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
  • 4
  • 3
  • 2
  • +4
15 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
Industry Leaders: 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

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!

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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