如何转换例如:Java中的(int)5到(byte)0x05?

Boo*_*hes 1 java hex byte

我试过了:

int amount = 5;
String amountStr = "0x0" + amount;
byte newByte = Byte.parsByte(amountStr);
Run Code Online (Sandbox Code Playgroud)

但我明白了java.lang.NumberFormatException: For input string: "0x05".

MBy*_*ByD 5

如果要解析String,请使用Byte#decode:

byte newByte = Byte.decode(amountStr);
Run Code Online (Sandbox Code Playgroud)

否则你可以投它(如评论中所写).