Changing 'System.out' encoding

Discussion in 'Java' started by Stanimir Stamenkov, Oct 15, 2004.

  1. I've searched the archives but haven't found any useful information.

    I'm trying to change the encoding of the default 'System.out' output
    stream so I could see some international characters in the text console.

    I'm trying this on Win2000:

    public class EncodingTest {

    public static void main(String[] args) {
    String cyrStr = "\u0430\u0431\u0432\u0433\u0434\u0435";
    String latStr = "\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB";

    System.out.println(System.getProperty("file.encoding"));
    System.out.println(cyrStr);
    System.out.println(latStr);
    JOptionPane.showMessageDialog(null, cyrStr + "\n" + latStr);
    System.exit(0);
    }

    }

    So my default encoding seems to be "Cp1251" (because of my regional
    settings) and on the command prompt I type:

    C:\>chcp 1251
    C:\>java EncodingTest

    and I get:

    Cp1251
    абвгде
    ??????

    then I want to see the latin supplement characters and I type:

    C:\>chcp 1252
    C:\>java -Dfile.encoding=Cp1252 EncodingTest

    and I get:

    Cp1252
    àáâãäå
    ??????

    So is it possible to change the encoding of the default 'System.out'?

    --
    Stanimir
     
    Stanimir Stamenkov, Oct 15, 2004
    #1
    1. Advertising

  2. Stanimir Stamenkov

    Yu SONG Guest

    Stanimir Stamenkov wrote:
    > I've searched the archives but haven't found any useful information.
    >
    > I'm trying to change the encoding of the default 'System.out' output
    > stream so I could see some international characters in the text console.
    >
    > I'm trying this on Win2000:
    >
    > public class EncodingTest {
    >
    > public static void main(String[] args) {
    > String cyrStr = "\u0430\u0431\u0432\u0433\u0434\u0435";
    > String latStr = "\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB";
    >
    > System.out.println(System.getProperty("file.encoding"));
    > System.out.println(cyrStr);
    > System.out.println(latStr);
    > JOptionPane.showMessageDialog(null, cyrStr + "\n" + latStr);
    > System.exit(0);
    > }
    >
    > }
    >
    > So my default encoding seems to be "Cp1251" (because of my regional
    > settings) and on the command prompt I type:
    >
    > C:\>chcp 1251
    > C:\>java EncodingTest
    >
    > and I get:
    >
    > Cp1251
    > абвгде
    > ??????
    >
    > then I want to see the latin supplement characters and I type:
    >
    > C:\>chcp 1252
    > C:\>java -Dfile.encoding=Cp1252 EncodingTest
    >
    > and I get:
    >
    > Cp1252
    > àáâãäå
    > ??????
    >
    > So is it possible to change the encoding of the default 'System.out'?
    >


    Redirect "System.out" to a *PrintStream*, so that you can change the
    encoding.

    --
    Song

    /* E-mail.c */
    #define User "Yu.Song"
    #define At '@'
    #define Warwick "warwick.ac.uk"
    int main() {
    printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
    return 0;}

    Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
    _______________________________________________________
     
    Yu SONG, Oct 15, 2004
    #2
    1. Advertising

  3. Stanimir Stamenkov

    John Davison Guest

    Stanimir Stamenkov wrote:

    > I'm trying to change the encoding of the default 'System.out' output
    > stream so I could see some international characters in the text console.


    -- snippy --

    > So is it possible to change the encoding of the default 'System.out'?


    If you look at the source code, you'll see that the System.out is final,
    so you can't change that without recompiling the library (and I
    wouldn't suggest doing that.) Here's what I came up with.

    import java.io.*;

    public class test {
    public static void main(String[] args) {

    PrintStream ps = null;

    try {
    ps = new PrintStream(System.out, true, "ISO-8859-1");
    } catch (UnsupportedEncodingException error) {
    System.err.println(error);
    System.exit(0);
    }

    ps.println("\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB");
    }
    }

    where "ISO-8859-1" is replaced by whatever character encoding you want.

    John Davison
    Compass Engineering Group
     
    John Davison, Oct 15, 2004
    #3
  4. Stanimir Stamenkov wrote:

    > I've searched the archives but haven't found any useful information.
    >
    > I'm trying to change the encoding of the default 'System.out' output
    > stream so I could see some international characters in the text console.


    [...]

    > So is it possible to change the encoding of the default 'System.out'?


    Lookup the type of System.out: it is a PrintStream, which is a subclass
    of OutputStream. OutputStreams are _byte_ streams. There is no
    relevant concept of a character encoding. You can, however, do this:

    PrintWriter out =
    new PrintWriter(
    new OutputStreamWriter(System.out, "Cp1251"));

    [You will need to be prepared to handle an UnsupportedEncodingException.]

    Thereafter write to your PrintWriter out, instead of directly to
    System.out, and you should be in good shape. [Note that I make no
    representation as to the console's ability to correctly display the
    resulting byte stream; I assume you have that under control.]


    John Bollinger
     
    John C. Bollinger, Oct 15, 2004
    #4
  5. /John C. Bollinger/:

    > PrintWriter out =
    > new PrintWriter(
    > new OutputStreamWriter(System.out, "Cp1251"));


    Huh... Thank you you, John (and thank you to John Davidson, too). :)

    You know guys, this was the first thing I've tried but in this form:

    Writer out = new OutputStreamWriter(System.out, "ISO-8859-1");
    out.write("\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB");

    and it just don't work (no output at all), that's why I've decided
    to post. But then using:

    PrintStream out = new PrintStream(System.out, true, "ISO-8859-1");
    out.println("\u00E0\u00E1\u00E2\u00E9\u00EA\u00EB");

    does it just fine - thank you, again.

    --
    Stanimir
     
    Stanimir Stamenkov, Oct 15, 2004
    #5
  6. Stanimir Stamenkov

    gshln

    Joined:
    Jan 2, 2013
    Messages:
    2
    My code which is simply casting the ascii character to get decimal value and it is working properly. For example:

    public static void main(String[] args) {
    char character = 'A';
    int intChar = (int) character;
    System.out.println("Decimal Value is " + intChar);
    }

    Which returns me correct output as below:
    Decimal Value is 65

    Above code is working fine when characters range is from 0 to 127 (US-ASCII). But when I am trying to get the decimal value for extended ascii characters it
    returns me wrong decimal number. For example

    public static void main(String[] args) {
    char character = 'Š';
    int intChar = (int) character;
    System.out.println("Decimal Value is " + intChar);
    }
    Which returns me incorrect output as below:
    Decimal Value is 352

    Instead it should return 138 as decimal value. 352 is the Unicode value for character 'Š' but I am expecting 138 which is correct value. Please help me to achieve this.
     
    gshln, Jan 2, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    5,340
    Steve
    Jul 1, 2003
  2. Hardy Wang

    Encoding.Default and Encoding.UTF8

    Hardy Wang, Jun 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    18,867
    Jon Skeet [C# MVP]
    Jun 9, 2004
  3. Replies:
    1
    Views:
    23,369
    Real Gagnon
    Oct 8, 2004
  4. JGH
    Replies:
    2
    Views:
    6,983
    Malte
    Feb 23, 2005
  5. Replies:
    2
    Views:
    373
Loading...

Share This Page