Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 240
Phillip Avatar asked Jan 12 '12 22:01

Phillip


People also ask

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

What is a null-terminated byte string?

A null-terminated byte string (NTBS) is a sequence of nonzero bytes followed by a byte with value zero (the terminating null character). Each byte in a byte string encodes one character of some character set.

How do you null-terminated strings?

The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by '\0'). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.

Which data type is a null-terminated character array?

A C-style string is a null (denoted by \0 ) terminated char array. The null occurs after the last character of the string. For an initialization using double quotes, "...", the compiler will insert the null .


2 Answers

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.

like image 99
Stephen C Avatar answered Oct 21 '22 21:10

Stephen C


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

like image 28
Eric Avatar answered Oct 21 '22 20:10

Eric