[英]Convert int to byte array
I need a way to do this 我需要一种方法来做到这一点
String username = "Snake";
int usernameLength = username.length(); // 5
converting it to 转换成
0x05
Should I use a for loop to get each number and add a zero if the result is less than two numbers? 如果结果少于两个数字,我应该使用for循环获取每个数字并添加零吗?
尝试ByteBuffer
类...
byte[] byteArray = ByteBuffer.allocate(1).putInt(username.length()).array();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.