I would like to transform a Java String str
into byte[] b
with the following characteristics:
b
is a valid C string ( ie it has b.length = str.length() + 1
and b[str.length()] == 0
. b
are obtained by converting the characters in str
to 8-bit ASCII characters. 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...
// 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:
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.