Convert from byte to char

HiIf I have a byte array, how can I convert this to a char as a specific encoding? lets say its UTF-16 or ASCII and I need to decide this on the fly.
[163 byte] By [invictus2a] at [2007-11-27 8:39:38]
# 1

I did this at work and I don't have the code here, but I think you can do something like

FileReader fr = new FileReader(file, "UTF-16");

and that will read UTF-16 characters.

If you don't have a file, and all you've got is a byte[], you can do this using a bytearrayinputstream and an InputStreamReader

tjacobs01a at 2007-7-12 20:37:39 > top of Java-index,Java Essentials,New To Java...
# 2

> If I have a byte array, how can I convert this to a

> char as a specific encoding? lets say its UTF-16 or

> ASCII and I need to decide this on the fly.

String s = new String(byteArray, encoding); // encoding = "utf-16" or "ASCII" or "utf-8" etc

sabre150a at 2007-7-12 20:37:39 > top of Java-index,Java Essentials,New To Java...