Convert zero to byte
I am reading a file and I want to print the byte value in 2 icons if it is less than 10 (example if byte = 1 it should use byte = 01), I don't want to compare it like this:
if(byte<10){
stringBuffer buf= new stringBuffer();
buf.append("0"+byte);
}
is there a built-in method for this, just like the format function in vc ++?
Thanks gagana
+2
gagana
source
to share
10 replies
You can use DecimalFormat
:
NumberFormat nf = new DecimalFormat("00");
buf.append(df.format(byteArray[i]));
Obviously, you are just instantiating it outside of the loop.
+3
source to share