Java String encoding

后端 未结 1 1337
一整个雨季
一整个雨季 2021-01-21 17:54

What´s the difference between

\"hello world\".getBytes(\"UTF-8\");

and

 Charset.forName(\"UTF-8\").encode(\"hello world\").arra         


        
相关标签:
1条回答
  • 2021-01-21 17:59

    Your second snippet uses ByteBuffer.array(), which just returns the array backing the ByteBuffer. That may well be longer than the content written to the ByteBuffer.

    Basically, I would use the first approach if you want a byte[] from a String :) You could use other ways of dealing with the ByteBuffer to convert it to a byte[], but given that String.getBytes(Charset) is available and convenient, I'd just use that...

    Sample code to retrieve the bytes from a ByteBuffer:

    ByteBuffer buffer = Charset.forName("UTF-8").encode("hello world");
    byte[] array = new byte[buffer.limit()];
    buffer.get(array);
    System.out.println(array.length); // 11
    System.out.println(array[0]);     // 104 (encoded 'h')
    
    0 讨论(0)
提交回复
热议问题