简体   繁体   English

有效地将Java字符串转换为表示C字符串的以空字符结尾的byte []? (ASCII)

[英]Efficiently convert Java string into null-terminated byte[] representing a C string? (ASCII)

I would like to transform a Java String str into byte[] b with the following characteristics: 我想将Java String str转换为byte[] b具有以下特征:

  • b is a valid C string ( ie it has b.length = str.length() + 1 and b[str.length()] == 0 . b是有效的C字符串( 它具有b.length = str.length() + 1并且b[str.length()] == 0
  • the characters in b are obtained by converting the characters in str to 8-bit ASCII characters. b中的字符是通过将str的字符转换为8位ASCII字符获得的。

What is the most efficient way to do this — preferably an existing library function? 最有效的方法是什么 - 最好是现有的库函数? Sadly, str.getBytes("ISO-8859-1") doesn't meet my first requirement... 可悲的是, str.getBytes("ISO-8859-1")不符合我的第一个要求......

// do this once to setup
CharsetEncoder enc = Charset.forName("ISO-8859-1").newEncoder();

// for each string
int len = str.length();
byte b[] = new byte[len + 1];
ByteBuffer bbuf = ByteBuffer.wrap(b);
enc.encode(CharBuffer.wrap(str), bbuf, true);
// you might want to ensure that bbuf.position() == len
b[len] = 0;

This requires allocating a couple of wrapper objects, but does not copy the string characters twice. 这需要分配几个包装器对象,但不会复制字符串字符两次。

You can use str.getBytes("ISO-8859-1") with a little trick at the end: 你可以在最后用一个小技巧使用str.getBytes("ISO-8859-1")

byte[] stringBytes=str.getBytes("ISO-8859-1");
byte[] ntBytes=new byte[stringBytes.length+1];
System.arrayCopy(stringBytes, 0, ntBytes, 0, stringBytes.length);

arrayCopy is relatively fast as it can use native tricks and optimizations in many cases. arrayCopy相对较快,因为在许多情况下它可以使用本机技巧和优化。 The new array is filled with null bytes everywhere we didn't overwrite it(basically just the last byte). 新数组在我们没有覆盖它的地方填充空字节(基本上只是最后一个字节)。

ntBytes is the array you need. ntBytes是您需要的阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM