I have array of bytes which is defined as pointer + size:
size_t size; // size in bytes
void *data; // NOT zero-terminated string
How do I construct, preferably zero-copy, 'string' from it?
You can simply iterate the byte array and print the byte using System. out. println() method.
Using String. getBytes() The String class provides three overloaded getBytes methods to encode a String into a byte array: getBytes() – encodes using platform's default charset.
Given a Byte value in Java, the task is to convert this byte value to string type. One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.
This assumes that data
points to immutable memory:
string s = (cast(immutable(char)*)data)[0..size];
If it doesn't, a char[]
would be more appropriate instead of a string, or you can make an immutable copy with .idup
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With