Reputation: 33
I am trying to write a library for android to encode and decode data.
Firstly I used Charset.defaultCharset().encode("Panagos").array();
and as the result I had the below sequence of bytes
byte[0] = 80
byte[1] = 97
byte[2] = 110
byte[3] = 97
byte[4] = 103
byte[5] = 111
byte[6] = 115
byte[7] = 0
byte[8] = 0
byte[9] = 0
byte[10] = 0
byte[11] = 0
byte[12] = 0
byte[13] = 0
and I noticed that the returned array had 7 extra 0 in the end.
Next I used "Panagos".getBytes();
and the result was
byte[0] = 80
byte[1] = 97
byte[2] = 110
byte[3] = 97
byte[4] = 103
byte[5] = 111
byte[6] = 115
and I don't know the reason.
So could you explain me (or to help me how to google) what are the differences between Charset.defaultCharset().encode().array();
and String.getBytes();
methods?
Thank you in advance,
Panagiotis
Upvotes: 0
Views: 284
Reputation: 73548
The issue is that Charset.encode()
returns a ByteBuffer
, and ByteBuffer.array()
returns the whole internal array, with zeroes in the end if the backing array is bigger than the contents of the buffer.
So which method to use? If you're working with Buffers
, then Charset.encode()
is useful. If you're working with arrays, then you'll want to use String.getBytes(charset)
.
Upvotes: 3