使用 Java 实现校验和


以下是使用 Java 实现校验和的代码 -

示例

 演示

import java.util.*;
public class Demo{
   public static void main(String args[]){
      Scanner my_scan = new Scanner(System.in);
      System.out.println("Enter the input string ");
      String my_in = my_scan.next();
      int my_checksum = generate_checksum(my_in);
      System.out.println("The checksum that has been generated is " + Integer.toHexString(my_checksum));
      System.out.println("Enter the data that needs to be sent to the receiver ");
      my_in = my_scan.next();
      System.out.println("Enter the checksum that needs to be sent to the receiver ");
      my_checksum = Integer.parseInt((my_scan.next()), 16);
      receive(my_in, my_checksum);
      my_scan.close();
   }
   static int generate_checksum(String s){
      String my_hex_val = new String();
      int x, i, my_checksum = 0;
      for (i = 0; i < s.length() - 2; i = i + 2){
         x = (int) (s.charAt(i));
         my_hex_val = Integer.toHexString(x);
         x = (int) (s.charAt(i + 1));
         my_hex_val = my_hex_val + Integer.toHexString(x);
         System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : " + my_hex_val);
         x = Integer.parseInt(my_hex_val, 16);
         my_checksum += x;
      }
      if (s.length() % 2 == 0){
         x = (int) (s.charAt(i));
         my_hex_val = Integer.toHexString(x);
         x = (int) (s.charAt(i + 1));
         my_hex_val = my_hex_val + Integer.toHexString(x);
         System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "+ my_hex_val);
         x = Integer.parseInt(my_hex_val, 16);
      } else {
         x = (int) (s.charAt(i));
         my_hex_val = "00" + Integer.toHexString(x);
         x = Integer.parseInt(my_hex_val, 16);
         System.out.println(s.charAt(i) + " : " + my_hex_val);
      }
      my_checksum += x;
      my_hex_val = Integer.toHexString(my_checksum);
      if (my_hex_val.length() > 4){
         int carry = Integer.parseInt(("" + my_hex_val.charAt(0)), 16);
         my_hex_val = my_hex_val.substring(1, 5);
         my_checksum = Integer.parseInt(my_hex_val, 16);
         my_checksum += carry;
      }
      my_checksum = generate_complement(my_checksum);
      return my_checksum;
   }
   static void receive(String s, int my_checksum){
      int gen_checksum = generate_checksum(s);
      gen_checksum = generate_complement(gen_checksum);
      int syndrome = gen_checksum + my_checksum;
      syndrome = generate_complement(syndrome);
      System.out.println("The value of syndrome is " + Integer.toHexString(syndrome));
      if (syndrome == 0){
         System.out.println("Data has been received without any errors");
      } else {
         System.out.println("An error was encountered in the received data");
      }
   }
   static int generate_complement(int my_checksum){
      my_checksum = Integer.parseInt("FFFF", 16) - my_checksum;
      return my_checksum;
   }
}

输入

sample
sample
b2c8

输出

Enter the input string
sa : 7361
mp : 6d70
le : 6c65
The checksum that has been generated is b2c8
Enter the data that needs to be sent to the receiver
Enter the checksum that needs to be sent to the receiver
sa : 7361
mp : 6d70
le : 6c65
The value of syndrome is 0
Data has been received without any errors

一个名为 Demo 的类包含了 main 函数。在此,定义了一个 Scanner 实例和一个输入字符串。

定义了 ‘generate_checksum’ 函数,它创建一个新的字符串实例并初始化 checksum 为 0。作为参数传递给此函数的字符串被迭代处理,并转换为十六进制值。每一个字符都按顺序迭代并转换为整数值。然后将其转换为十六进制值。然后,将该十六进制值添加到下一个字符的十六进制值。

如果字符串的长度为偶数,则会按顺序迭代处理每一个字符并将其转换为整数值。然后将其转换为十六进制值。然后,将该十六进制值添加到下一个字符的十六进制值。否则,它会与 ‘00’ 连接起来。

‘receive’ 函数调用 ‘generate_checksum’ 函数并传递结果。然后将其添加到原始 checksum 值(最初为 0)。然后将其转换为十六进制值。如果这个长度大于 4,那么就会生成一个 ‘carry’ 值。原始 checksum 会被解析,并添加到 ‘carry’ 值。一旦这段代码块执行,‘generate_complement’ 函数就会对 checksum 值进行调用。‘generate_complement’ 函数从 ‘FFFF’-16 中减去 checksum 值。

更新于: 2020 年 7 月 4 日

2K+ 浏览次数

开启你的 职业生涯

完成课程,获得证书

开始
广告