> From: Len Popp [mailto:len.p...@gmail.com]
> Subject: Re: [OT] Basic int/char conversion question
>
> If there's an easy way to convert a single character,
> someone please point it out.

Not particularly easy, but this should work:

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.charset.Charset;

public class Converter {
  byte[] ba = new byte[1];
  InputStreamReader isr;

  public Converter(String csName) {
    isr = new InputStreamReader(new ByteArrayInputStream(ba), 
Charset.forName(csName));
  }

  public char convert(byte b) {
    try {
      isr.reset();
      ba[0] = b;
      return (char)isr.read();
    } catch (IOException ioe) {
      // This can't happen in our situation, but...
      return '\0';
    }
  }
}

The calling program merely has to instantiate a Converter once for the 
character set of interest, then call the convert method to translate the byte:

    cvt = new Converter("ISO-8859-2");
...
    myChar = cvt.convert(myByte);

This of course only works for 8-bit character sets.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to