• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.nio.ByteBuffer to StringBuffer  RSS feed

 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Harald Kirsch
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!