Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Changing 'System.out' encoding

Reply
Thread Tools

Changing 'System.out' encoding

 
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      10-15-2004
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.encodi ng"));
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
 
Reply With Quote
 
 
 
 
Yu SONG
Guest
Posts: n/a
 
      10-15-2004
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.encodi ng"));
> 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/
__________________________________________________ _____

 
Reply With Quote
 
 
 
 
John Davison
Guest
Posts: n/a
 
      10-15-2004
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
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      10-15-2004
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


 
Reply With Quote
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      10-15-2004
/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
 
Reply With Quote
 
gshln gshln is offline
Junior Member
Join Date: Jan 2013
Posts: 2
 
      01-02-2013
My code which is simply casting the ascii character to get decimal value and it is working properly. For example:

publicstaticvoid 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

publicstaticvoid 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading Text File Encoding and converting to Perls internal UTF-8 encoding sln@netherlands.com Perl Misc 2 04-17-2009 11:22 PM
Why is ASP.NET changing character encoding of documents? John Dalberg ASP .Net 2 10-17-2005 12:15 AM
changing string encoding mannux@noos.fr ASP .Net 0 09-06-2005 07:32 PM
changing JVM encoding; setting -Dfile.encoding doesn't work pasmol@plusnet.pl Java 1 10-08-2004 09:50 PM
Encoding.Default and Encoding.UTF8 Hardy Wang ASP .Net 5 06-09-2004 04:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57