Question
Create a program to produce 8, 16, and bit 32 checksums of text input. All characters are valid (ASCII values): Includes punctuation, periods, whitespace, NEWLINE
Create a program to produce 8, 16, and bit 32 checksums of text input. All characters are valid (ASCII values): Includes punctuation, periods, whitespace, NEWLINE character, and more. Print the checksum result in the following format: "%d bit checksum is %d", where the first placeholder holds the bit size (8,16,32) and the second the calculated checksum (hex value).
Note that the result is masked to print the appropriate sizes such as two hex characters for 8 bits, four hex characters for 16 bits, and 8 hex for 32 bit.
Answer Key Examples:
"The highest forms of understanding we can achieve are laughter and human compassion. ? Richard Feynman "
// 8 bit checksum is 38 // 16 bit checksum is 9baf // 32 bit checksum is 749f7f72
"A love for tradition has never weakened a nation, indeed it has strengthened nations in their hour of peril. Sir Winston Churchill " // 8 bit checksum is e3 // 16 bit checksum is 5d9d // 32 bit checksum is ef00c701
Memo: I have been able to sucessfully output the 8bit checksum using my code. If you can modify it to produce 16bit and 32bit it would be helpful. Also commenting your additions will help me learn (if you have the time).
My code:
String input = "A love for tradition has never weakened a nation, indeed it has strengthened nations in their hour of peril. Sir Winston Churchill ";
int checkSumSize = 0, characterCount, checkSum = 0;
if (input != null) { byte[] fileBytes = input.getBytes(); characterCount = fileBytes.length; checkSumSize = 8; checkSum = checksum(fileBytes, checkSumSize) & 0xFF; System.out.println(formattedString(input) + " " + checkSumSize + " bit checksum is " + Integer.toHexString(checkSum) + " for all " + characterCount + " chars"); }
private static int checksum(byte[] data, int shift){ //shift = 8 for 8bit result. byte a = 0, b = 0; for (int i = 0; i < data.length; i++) { a = (byte) ((a + data[i]) % 255); b = (byte) ((a + b) % 255); } return ((b << shift) | a); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started