Andreas
Andreas

Reputation: 5335

Weird String.getByte(Encoding)

I am trying this:

byte[] b = String.getBytes("ASCII") and get an UnsupportedEncodingException Exception

String fName = new String(b,"ASCII");

- got the same when used

byte[] b = String.getByte("ISO8859_1");
String fName = new String(b,"ISO8859_1");

edit: getByte changed to getBytes

Upvotes: 1

Views: 5985

Answers (4)

Tom
Tom

Reputation: 8180

Although worth noting this still unresolved bug...

"EXPECTED VERSUS ACTUAL BEHAVIOR :

Ludicrous performance degradation when using encoding of "US-ASCII" relative to encoding of "ASCII". There is absolutely no reason why developers should expect such wildly different performance profiles when executing this code for these two encodings."

Upvotes: 0

Simon Nickerson
Simon Nickerson

Reputation: 43159

I think you want something more like this:


import java.io.UnsupportedEncodingException;


public class Encoding
{
    public static void main(String[] args) throws UnsupportedEncodingException
    {
        String s = "Hello world";
        byte[] b = s.getBytes("US-ASCII");
    }
}

Upvotes: 2

dfa
dfa

Reputation: 116334

according to javadoc you must use: "US-ASCII"

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500385

That code won't compile - it's String.getBytes() not String.getByte(), and it's an instance method not a static method. It's always worth cutting and pasting a real example which you've got working (even if it's just a dummy app).

However, assuming you've got similar code which is compiling, you should be using "US-ASCII" and "ISO-8859-1" as the names, as documented in the Charset JavaDoc.

Upvotes: 7

Related Questions