简体   繁体   中英

Creating string from byte[] not working the same on Ubuntu as on Windows java

public class chartests {
    public static void main(String[] args){
        byte char255 = -5;
        byte[] single = new byte[1];
        single[0] = char255;
        String output = new String(single);
        System.out.println(output);
    }
}

This code demonstrates a problem I am having with a little server application I am making.

So, when I run this on my windows machine, a 'u' character with an accent over it is printed out. When I run this on my Ubuntu a machine a '?' unknown character is printed out.

Obviously this is because the Ubuntu machine is not supporting characters outside the 127 ascii range.

How can I explicitly encode and decode my strings using a certain encoding? I want an encoding which is exactly one byte per character as well, as I'm more concerned with what bytes are coming across than what characters, and don't want to deal with the headache of having more than one byte per character in my parser. I'm not certain UTF-8 would be acceptable either.

The behavior I have on my windows machine is what I want to happen on Ubuntu. How can I get my Ubuntu machine to decode the -5 byte as an accented u?

The constructor java.lang.String(byte[] bytes, String charsetName) could make sure your code work as expected. For example:

new String(new byte[]{-5},"ISO8859-1")

If you ignore the charsetName parameter, the default charset will be System property file.encoding . The value maybe different on different environment. This rule also apply to java.io.InputStreamReader and other API which supply charsetName parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM