一、代码
/**
* @author Alan Chen
* @description 十六进制计算
* @date 2024-02-21
*/
public class HexCalcClient {
public static void main(String[] args) {
String hex = plus();
lowEightBits(hex);
}
/**
* 十六进制数组累加
*/
private static String plus() {
String hexStr = "55 AA 02 06 00 64";
String[] hexArray = hexStr.split(" ");
if (hexArray.length == 0) {
return "0";
}
int sum = 0;
for (String hex : hexArray) {
int num = Integer.parseInt(hex, 16);
sum = sum + num;
}
String resultHex = Integer.toHexString(sum).toUpperCase();
System.out.println("相加结果:" + resultHex);
return resultHex;
}
/**
* 取十六进制的低八位
*/
private static void lowEightBits(String hex) {
int decimal = Integer.parseInt(hex, 16);
int lowEightBits = decimal % 256;
String resultHex = Integer.toHexString(lowEightBits).toUpperCase();
System.out.println("低八位的十六进制表示为:" + resultHex);
}
}
二、结果
相加结果:16B
低八位的十六进制表示为:6B
网友评论