This has probably been answered else where but how do you get the character value of an int value?
Specifically I’m reading a from a tcp stream and the readers .read() method returns an int.
How do I get a char from this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re trying to convert a stream into text, you need to be aware of which encoding you want to use. You can then either pass an array of bytes into the
Stringconstructor and provide aCharset, or useInputStreamReaderwith the appropriateCharsetinstead.Simply casting from
inttocharonly works if you want ISO-8859-1, if you’re reading bytes from a stream directly.EDIT: If you are already using a
Reader, then casting the return value ofread()tocharis the right way to go (after checking whether it’s -1 or not)… but it’s normally more efficient and convenient to callread(char[], int, int)to read a whole block of text at a time. Don’t forget to check the return value though, to see how many characters have been read.