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

Can a Byte be converted to String?

 
smyle khanna
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,,
I have a very basic doubt whethere a byte can be converted to a String.
and can it be stored in a string since when i tried doing tht it gave errors sayin incompatible types.
i did it this way String=Byte.toString(b);where b=2 or so.
but Byte.toString() in a if loop executes without any errors...
please xplain me n provide me a code also..
thnaks
Smyle
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
Darin Niard
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There's no reason that either of those wouldn't work...
 
Kalai Selvan
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi smyle,



Kalai Selvan T.
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darin Niard:
[QBThere's no reason that either of those wouldn't work...[/QB]


Precisely.

smyle, the mistake in your code is probably something else and not because of the syntax
Byte.toString(b);
 
smyle khanna
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks 4 the replies .i got the code working.
actually i was trying to convert the
Byte b=new Byte("127");
into
String str=Byte.toString(b);
which was giving a error..
Error: toString() in java.lang.Object cannot be applied to (java.lang.Byte)

Cant we convert a wrapper class into another wrapper class?


thanks
Smyle
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by smyle khanna:
Hi
Thanks 4 the replies .i got the code working.
actually i was trying to convert the
Byte b=new Byte("127");
into
String str=Byte.toString(b);
which was giving a error..
Error: toString() in java.lang.Object cannot be applied to (java.lang.Byte)

Cant we convert a wrapper class into another wrapper class?


thanks
Smyle


Byte is a wrapper class; String is not.
The error in your code is that you are using the static toString() method in Byte class that takes byte (primitive) as an argument and not Byte (wrapper) object.

What you need is the instance toString() method which doesn't take any argument but returns the String representation of the Byte object. If you insist on using the static toString() method, then you will need to 1st convert the Byte into byte via the byteValue() method:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic