This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes java.nio.ByteBuffer to StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "java.nio.ByteBuffer to StringBuffer" Watch "java.nio.ByteBuffer to StringBuffer" New topic
Author

java.nio.ByteBuffer to StringBuffer

Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89
Hi,

I want to convert java.nio.ByteBuffer object to a StringBuffer.

I have tried using getChar(int index) method of java.nio.ByteBuffer
and then tried to append each char to the StringBuffer.
But, when I print the contents of StringBuffer using SOP, it prints a series of '?'.

Is there any other way of converting from java.nio.ByteBuffer to StringBuffer ?

Please help me out.

Thanks,
Nidhi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
You need to convert from bytes to characters. This will be determined by what sort of character encoding was used for the bytes. Take a look at the Charset class and the related CharsetEncoder and CharsetDecoder.


"I'm not back." - Bill Harding, Twister
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780


There is no emoticon for what I am feeling!
Harald Kirsch
Ranch Hand

Joined: Oct 14, 2005
Posts: 37
Originally posted by Nidhi Singhal:

I want to convert java.nio.ByteBuffer object to a StringBuffer.


I once wondered why this does not exit. Could not believe it. Finally I did it myself.

monq.stuff.Coder

Emma test coverage reports 41.2/42 lines code coverage for my junit tests. This does not mean it must be free of bugs, but I made at least an effort to get it right.

Download link at the bottom of the page mentioned above.


Harald.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: java.nio.ByteBuffer to StringBuffer
 
Similar Threads
StringBuffer and equals()
StringBuffer class
Conversion ?
How do I convert a big-endian(64 bit) to little endian(64 bit) in Java
2 bytes converted a Short