将十六进制字符串转换为2字节有符号整数的Java代码
要将输出从形式为'33303236304a4a4a4a4a4a3a315b2d36352c20352c20302c2'的十六进制字符串转换为2字节的有符号整数,你可以使用以下代码修改你的代码:
try {
AssetManager assetManager = context.getAssets();
inputStream = assetManager.open(FILE_NAME);
int fileByteSize = inputStream.available();
// Log.i(TAG, "bytesize is " + fileByteSize);
if (fileByteSize > 0) {
byte[] bytes = new byte[fileByteSize];
inputStream.read(bytes);
decodeSignatureInfo(bytes);
// releaseSignature = Arrays.toString(bytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.length; i += 2) {
String hex = new String(bytes, i, 2);
int value = Integer.parseInt(hex, 16);
builder.append(value);
if (i < bytes.length - 2) {
builder.append(", ");
}
}
releaseSignature = builder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
在这段代码中,我使用了一个循环来迭代每两个字节,并将它们转换为有符号整数。然后,我将这些整数添加到StringBuilder中,并在每个整数之间添加逗号和空格。最后,我将StringBuilder转换为字符串并将其赋值给releaseSignature变量。请确保将以上代码放入try-catch块中以处理可能的IOException。
原文地址: https://www.cveoy.top/t/topic/qeYB 著作权归作者所有。请勿转载和采集!