0% found this document useful (0 votes)
19 views4 pages

5 48

The document contains a C++ program that implements a checksum calculation using a 4-bit Internet checksum method. It includes functions to add binary numbers with carry and to calculate the checksum of binary data, while also validating user input. The program simulates both sender and receiver sides, verifying the integrity of the received data against the calculated checksum.

Uploaded by

swetaku7091
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

5 48

The document contains a C++ program that implements a checksum calculation using a 4-bit Internet checksum method. It includes functions to add binary numbers with carry and to calculate the checksum of binary data, while also validating user input. The program simulates both sender and receiver sides, verifying the integrity of the received data against the calculated checksum.

Uploaded by

swetaku7091
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <string>

using namespace std;

string addWithCarry(string a, string b) {

int sum = 0;

string result = "";

for (int i = 3; i >= 0; i--) {

sum += (a[i] - '0') + (b[i] - '0'); // Add the corresponding bits of a


and b

result = (sum % 2 == 0 ? "0" : "1") + result; // Append sum modulo


2 (current bit)

sum /= 2; // Carry is the quotient after dividing by 2 (this will either


be 1 or 0)

// Return the result as a 4-bit sum

return result;

// Function to calculate checksum using the 4-bit Internet checksum


method

string calculateChecksum(const string &binaryData) {

string checksum = "";

string sum = "0000"; // Initialize sum as 0000 (4-bit)

// Process the data in 4-bit chunks

for (size_t i = 0; i < binaryData.length(); i += 4) {

string chunk = binaryData.substr(i, 4); // Get 4-bit chunk


// Add the chunk to the sum (handle carry within 4 bits)

sum = addWithCarry(sum, chunk);

cout << "Sender: Chunk " << chunk << " - Sum (with carry): " <<
sum << endl;

// Take 1's complement of the final sum (invert all bits)

string complement = "";

for (char bit : sum) {

complement += (bit == '1' ? '0' : '1');

// The complement is the checksum

checksum = complement;

cout << "Sender: Final checksum (1's complement of sum): " <<
checksum << endl;

return checksum;

int main() {

string data; // Binary data to be sent

string sentChecksum, receivedChecksum;

// Sender Side

cout << "Enter the binary data to send (only 0's and 1's): ";

cin >> data;

// Validate the input binary data


for (char ch : data) {

if (ch != '0' && ch != '1') {

cout << "Invalid input! Only binary digits (0 or 1) are allowed." <<
endl;

return 1; // Exit if input is invalid

// Calculate checksum of data to be sent (binary sum + 1's complement


checksum)

sentChecksum = calculateChecksum(data);

cout << "Final checksum (sent to receiver): " << sentChecksum <<
endl;

// Send both the data and checksum to the receiver (simulated by


entering the checksum)

cout << "Enter the received data: ";

string receivedData;

cin >> receivedData;

cout << "Enter the received checksum: ";

cin >> receivedChecksum;

// Receiver Side - Verification

cout << "Receiver receives checksum: " << receivedChecksum <<


endl;

cout << "Receiver receives data: " << receivedData << endl;

// Calculate checksum on the receiver side using the same method

string receiverSum = "0000"; // Initialize sum as 0000 (4-bit)


// Receiver processes the data in chunks

for (size_t i = 0; i < receivedData.length(); i += 4) {

string chunk = receivedData.substr(i, 4); // Get 4-bit chunk

// Add the chunk to the sum (handle carry within 4 bits)

receiverSum = addWithCarry(receiverSum, chunk);

cout << "Receiver: Chunk " << chunk << " - Sum (with carry): " <<
receiverSum << endl;

// Take 1's complement of the final sum (receiver's checksum)

string receiverComplement = "";

for (char bit : receiverSum) {

receiverComplement += (bit == '1' ? '0' : '1');

cout << "Receiver: Final checksum (1's complement of sum): " <<
receiverComplement << endl;

// Compare the received checksum with the calculated checksum

if (receivedChecksum == receiverComplement) {

cout << "Data received correctly (no error)." << endl;

} else {

cout << "Data error detected! Checksum mismatch." << endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy