Converting an ASCII character

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
DanJWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
char c = (int)0x50 ;

int i = (int)'a' ;

0
DanJWAuthor Commented:
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
TimYatesCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CEHJCommented:
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
DanJWAuthor Commented:
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
Mayank SAssociate Director - Product EngineeringCommented:
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
functionpointerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanJWAuthor Commented:
Erm, a bit old but I forgot.  I did use InputStreamReader etc. in the end.  Thanks.

Dan
0
CleanupPingCommented:
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
jimmackCommented:
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
TimYatesCommented:
I would suggest functionpointer gets the points :-)
0
CEHJCommented:
Yep
0
jimmackCommented:
Apologies to functionpointer.  I mis-interpretted the last comment from DanJW.

TimYates and CEHJ - Thank you ;-)

Points to functionpointer.
0
TimYatesCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.