Trimming byte array when converting byte array to string in Java/Scala
- by prosseek
Using ByteBuffer, I can convert a string into byte array:
val x = ByteBuffer.allocate(10).put("Hello".getBytes()).array()
> Array[Byte] = Array(104, 101, 108, 108, 111, 0, 0, 0, 0, 0)
When converting the byte array into string, I can use new String(x).
However, the string becomes hello?????, and I need to trim down the byte array before…