wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can a Byte be converted to String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can a Byte be converted to String?" Watch "Can a Byte be converted to String?" New topic
Author

Can a Byte be converted to String?

smyle khanna
Ranch Hand

Joined: Jan 14, 2004
Posts: 54
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

Joined: Jul 04, 2004
Posts: 195
Try this:



Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118


There's no reason that either of those wouldn't work...
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi smyle,



Kalai Selvan T.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
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

Joined: Jan 14, 2004
Posts: 54
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

Joined: Nov 26, 2003
Posts: 382
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:


Ever Existing, Ever Conscious, Ever-new Bliss
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can a Byte be converted to String?