我需要一种方法来做到这一点
String username = "Snake";
int usernameLength = username.length(); // 5
将其转换为
0x05
我是否应该使用 for 循环来获取每个数字,如果结果少于两个数字,则添加一个零?
最佳答案
尝试ByteBuffer
类...
byte[] byteArray = ByteBuffer.allocate(1).putInt(username.length()).array();
关于java - 将 int 转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758707/