使用 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 值。
廣告