如何将带有空终止字符的字节数组转换为Java中的字符串? [英] How do I convert a byte array with null terminating character to a String in Java?
问题描述
如何从字节数组创建一个String对象
byte arr [MAX_SIZE] // Java
其中一个数组元素是C null终止字节?就像调用
一样简单 String str = new String(arr);
String构造函数是否知道会在null终止字符处自动停止?空字节后的任何字节都是(可能)无用的字符,我不想包括在字符串中。 解析Java中的字节数组中的最后一个响应建议循环访问数组,并手动查找null终止字符,但我想知道String构造函数是否会自动执行。我还假设系统的默认字符集将在所有的端点使用。
[] = ...
Charset charset = ...
int i;
for(i = 0; i String str = new String(arr,0,i,charSet) ;
注意:
-
使用显式的CharSet参数通常是个好主意,因此您的应用程式不依赖于平台的预设字元/编码。
- p>这不适用于某些字符集。例如,UTF-16编码的字符串不能安全地表示为零终止的字节序列,因为许多代码单元包含零字节。 (另一方面,如果字符串不包含代码点零的实例,则UTF-8是正常的;参见 UTF-8是否包含零字节?)
...但我想知道String构造函数是否会自动执行此操作。
不,它们不会。 (不要奇迹...读取javadoc: - ))
我也假设系统的默认字符集所有结束。
如果不指定字符集,将使用Java平台的默认值。这可能是系统的默认值,但不能保证。
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 in java 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.
byte arr[] = ...
Charset charset = ...
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.
这篇关于如何将带有空终止字符的字节数组转换为Java中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!