This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a null terminated string from a ByteBuffer/CharBuffer

 
Larry Cha Cha
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you feel that you need a null-terminated string?
bear
 
Larry Cha Cha
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic