wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Getting a null terminated string from a ByteBuffer/CharBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting a null terminated string from a ByteBuffer/CharBuffer" Watch "Getting a null terminated string from a ByteBuffer/CharBuffer" New topic
Author

Getting a null terminated string from a ByteBuffer/CharBuffer

Larry Cha Cha
Greenhorn

Joined: Jun 06, 2003
Posts: 15
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Why do you feel that you need a null-terminated string?
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Larry Cha Cha
Greenhorn

Joined: Jun 06, 2003
Posts: 15
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Getting a null terminated string from a ByteBuffer/CharBuffer
 
Similar Threads
String into byte buffer
Converting from ISO-8859-1 to UTF-8
Getting null-terminated ASCII string from ByteBuffer
Reading file using FileChannel
NX URLyBird 1.3.2: Extracting null terminated strings from ByteBuffer