How do I convert a byte array with null terminating character to a String in Java?

前端 未结 4 1426
忘掉有多难
忘掉有多难 2020-12-03 13:38

How can I create a String object from a byte array

byte arr[MAX_SIZE];  // Java

where one of the array elements is a C null terminating byt

4条回答
  •  隐瞒了意图╮
    2020-12-03 14:00

    It won't magically stop at the null terminator. The null character doesn't terminate strings in Java. You will have to find the index of the first null character and stop there. Use the String(byte[] arr, int offset, length) constructor after that.

提交回复
热议问题