I have a byte in java and I need it in String.
byte myByte = 0x01;
String num = myByte.toString();
Something like that or I don't know, first I transform it to int and then to String with String.valueof(int)
But how do I first convert a byte to an integer? I have read how to do it from byte array but if I only have one byte?
The class
String
has the methodvalueOf()
overloaded to be able to convert the primitive types toString
in a very simple way. Although there is no specific one that receives a type parameterbyte
, the compiler does a promotion toint
and invokes the version that receives an integer.You can do it this way:
It is also important to define an encoding to avoid encoding problems, which can be done in this way
To get the String representation or using just
String.valueOf()
, like this:I add the link of a similar question on the English site, where you can see several options to do this:
Convert byte to String.