我有一个应该读取十六进制数字的函数,但它没有正确读取它。多项式函数将字符串读取为 ASCII 而不是十六进制。
这是正在执行工作的代码部分:
JButton button = new JButton("Calculate");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = textArea.getText();
int crc = 0xFFFF;
int polynomial = 0x1021;
byte bytes[] = str.getBytes();
for (byte b : bytes) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xFFFF;
textField.setText(""+Integer.toHexString(crc));
}
});
button.setBounds(10, 245, 90, 25);
panel.add(button);