Posted By: Anonymous
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?
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
String constructor and provide a
Charset, or use
InputStreamReader with the appropriate
Simply casting from
char only 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 of
char is the right way to go (after checking whether it’s -1 or not)… but it’s normally more efficient and convenient to call
read(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.