转换为字节时,字符串"00"与数字0x00不同.您尝试在字节数组前面添加的数据类型是什么?假设它是字符串"00"的字节表示,请尝试以下操作:
bytes[] orig = <your byte array>;
String prepend = "00";
bytes[] prependBytes = prepend.getBytes();
bytes[] output = new Bytes[prependBytes.length + orig.length];
for(i=0;i<prependBytes.length;i++){
output[i] = prependBytes[i];
}
for(i=prependBytes.length;i<(orig.length+prepend.lenth);i++){
output[i] = orig[i];
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用Arrays.copy(...)代替前面提到的两个for循环来进行前置.请参见如何组合两个字节数组
另外,如果你试图在字面数组前面加上0,则按prependBytes以下方式进行decalare 并使用相同的算法
byte[] prependBytes = new byte[]{0,0};
Run Code Online (Sandbox Code Playgroud)
另外你说你将你的字节数组转换为十六进制,这可能会截断前导零.要对此进行测试,请尝试预先设置以下内容并转换为十六进制,然后查看是否存在不同的输出:
byte[] prependBytes = new byte[]{1,1};
Run Code Online (Sandbox Code Playgroud)
如果它正在删除您想要的前导零,您可能希望将十六进制数转换为字符串并对其进行格式化.
| 归档时间: |
|
| 查看次数: |
4948 次 |
| 最近记录: |