我想读取 NFC 标签的 block 数据。其中命令是字节数组,需要 block 号。
public static byte[] readSingleBlockCmd(int blockNo) {
byte[] cmd = new byte[3];
cmd[0] = (byte) 0x02;//flag
cmd[1] = (byte) 0x23;//cmd
cmd[2]= (byte)blockNo;
return cmd;
}
- How can I change the int blockNo to its hexadecimal value , which can be cast to byte .I want the byte value and not an byte []
我已浏览以下链接
Convert integer into byte array (Java)
How to autoconvert hexcode to use it as byte[] in Java?
谢谢!
最佳答案
可以使用以下行将整数(十进制)转换为十六进制:
String hex = Integer.toHexString(blockNo);
然后将其转换为字节,您可以使用
Byte.parseByte(hex,16);
但是如果您只想将参数转换为字节:
Byte.parseByte(blockNo);
我想也可以。如果我错了请纠正我。
关于java - 将 int 转换为十六进制字节值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30464835/