Forums Register Login

Getting a null terminated string from a ByteBuffer/CharBuffer

+Pie Number of slices to send: Send
How would I go about getting a string from a ByteBuffer or CharBuffer with the same contents, that is null terminated without checking every single character?
Cheers.
+Pie Number of slices to send: Send
Well, there's

Here indexOf() is checking every character for you. I don't know of another shortcut - you have to check every character. Unlike C, Java does not normally use nulls to signal the end of a String, and so the libraries don't bother to support it. But it's pretty easy to do yourself.
+Pie Number of slices to send: Send
Why do you feel that you need a null-terminated string?
bear
+Pie Number of slices to send: Send
Thanks Jim I was hopeing there was some util method that would do it but your snippet should do me fine.
Bear: It's from an existing file that stores their strings as null terminated, I wouldn't have done it myself that way.
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2970 times.
Similar Threads
Reading file using FileChannel
String into byte buffer
Converting from ISO-8859-1 to UTF-8
NX URLyBird 1.3.2: Extracting null terminated strings from ByteBuffer
Getting null-terminated ASCII string from ByteBuffer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:39:24.