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?
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?
Simple casting:
int a = 99;
char c = (char) a;
Is there any reason this is not working for you?
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 Charset
instead.
Simply casting from int
to 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 read()
to 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.
Hi,
Maybe you are asking for:
Character.toChars(65) // returns ['A']
More info: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#toChars(int)