28

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 byte? Is it as simple as calling

String str = new String( arr );

Will the String constructor know to automatically stop at the null terminating character? Any bytes after the null byte are (possibly) garbage characters that I don't want to include in the string. The last response under Parsing byte array containg fields of unknown length suggests looping through the array and manually finding the null terminating character, but I was wondering whether the String constructor will do this automatically. I also assume the system's default charset will be used on all ends.

4 Answers 4

23
byte arr[] = ...
Charset charset = ...

// Find the position of the first zero byte
int i;
for (i = 0; i < arr.length && arr[i] != 0; i++) { }


String str = new String(arr, 0, i, charSet);

Notes:

  • It is generally a good idea to use an explicit CharSet parameter so that your application doesn't depend on the platform's default characterset / encoding.

  • This won't work for some charsets. For instance, a UTF-16 encoded string can't safely be represented as a zero-terminated byte sequence because many code units contain zero bytes. (On the other hand, UTF-8 is OK provided that the string contains no instances of code point zero; see Can UTF-8 contain zero byte?)

... but I was wondering whether the String constructor will do this automatically.

No it / they won't. (Don't "wonder" ... read the javadoc :-))

I also assume the system's default charset will be used on all ends.

If you don't specify a charset, the Java platform's default will be used. This is likely to be the system default, but that is not guaranteed.

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

Comments

14

try this: String s = new String(arr).trim()

3 Comments

trim() removes the null character at the end of my strings just fine...
trim() will only work if all characters at the end of the array are zero (e.g [65, 66, 67, 0, 0, 0]: this converts to "ABC") but not with junk characters after the terminator (e.g. [65, 66 ,67, 0, -12, 10 , 33]).
For any Kotlin dev that might stumble upon this: the trim() works only for java.lang.String, it doesn't work for regular Kotlin strings.
12

how about this:

String str = new String(arr).split("\0")[0];

2 Comments

Doesn't look too good if the input array is huge, unless you are going to read all the strings anyway.
This is making assumptions about the default charset. And it is creating a large temporary string, an array an potentially a large number of unnecessary strings for the stuff after the first null.
4

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.

2 Comments

The NULL char is not necessarily toward the end of the array; it can be at any position.
@Phillip, realised this after reading your question again.. edited my answer

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.