1
public static void main(String args[])
    {       
        byte[] bytearray = new byte[]{1, -30, 48, 50, 49, 48};

        for (int i = 0; i < bytearray.length; i++) {
            System.out.print(bytearray[i]+", ");
        }
        System.out.println();
        System.out.println("Length of byteArray before : "+bytearray.length);

        String st = new String(bytearray);
        System.out.println("String value : "+ st);  

        bytearray = st.getBytes();

        for (int i = 0; i < bytearray.length; i++) {
            System.out.print(bytearray[i]+", ");
        }
        System.out.println();
        System.out.println("Length of byteArray after : "+bytearray.length);
    }

this is my program if i am executing this on windows i am getting exactly same bytes as previous but on ubuntu, it's giving extra 2 bytes I didn't understand it ? why ?

which method should I use to get same array on ubuntu ?

1
  • I think you can fix that by using the same character set. Here is an example: new String(bytearray, "UTF-8") and str.getBytes("UTF-8"). Commented Oct 24, 2016 at 10:59

2 Answers 2

2

Sorry, I thought you were using valid encoded data.

Your bytes are not valid UTF-8, so if you have different versions of Java it may handle this differently.

new byte[]{1, -30, 48, 50, 49, 48}

In short your should be using text to attempt to store binary data. This will only confuse you (and waste memory)

Sign up to request clarification or add additional context in comments.

6 Comments

I am working on ubuntu and it's throwing java.io.UnsupportedEncodingException if I am using StandardCharsets.UTF_8
@Shailesh There is something wrong with your JVM then, as it's a standard builtin encoding all JVMs have to have which is why it's in the StandardCharsets. docs.oracle.com/javase/8/docs/api/index.html?java/nio/charset/… note: If you use a String you have to write "UTF-8"
it's giving default charset as utf-8, i have tried using "UTF-8" still it's not working..! are you sure there is something wrong with my JVM ?
sorry but I didn't get that ideone.com link program how it shows JVM is wrong ?
@Shailesh Sorry, I realised what the problem is. You can't be encoding binary data as text unless you like confusion (or you really know what you are doing) I strongly suggest you avoid trying to do this. Different versions of Java and different encoding handle invalid encoded bytes differently.
|
1

Here is what the documentation says for String constructor:

Constructs a new String by decoding the specified array of bytes using the platform's default charset.

And what it says for getBytes:

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

So, why do you get different behavior in different systems? Because their default charsets are different. You can get the default charset using Charset.defaultCharset().

3 Comments

I have tried this it's giving default charset as UTF-8, and still the problem exist...!
Try using String st = new String(bytearray, "ISO-8859-1") and bytearray = st.getBytes( "ISO-8859-1")
Even though you can do this, it's likely to be a bad idea unless you know the bytes are a String encoded as ISO-8859-1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.