Question: Make this C + + program to run multiple time. #include #include #include / / Function to convert hexadecimal string to decimal integer unsigned int

Make this C++program to run multiple time.
#include
#include
#include
// Function to convert hexadecimal string to decimal integer
unsigned int hexToDec(const std::string& hexStr){
unsigned int result;
std::stringstream ss;
ss << std::hex << hexStr;
ss >> result;
return result;
}
// Function to convert hexadecimal IP address to decimal IP address
std::string hexIPToDec(const std::string& hexIP){
std::string decIP;
for (size_t i =0; i < hexIP.size(); i +=2){
std::string byteStr = hexIP.substr(i,2);
unsigned int byte = hexToDec(byteStr);
decIP += std::to_string(byte);
if (i < hexIP.size()-2){
decIP +=".";
}
}
return decIP;
}
int main(){
std::cout << "Enter the hexadecimal IP datagram header: ";
std::string header;
std::cin >> header;
// Validate input length
if (header.length()!=20){
std::cout << "Error: Illegal header size" << std::endl;
return 1;
}
// Extract information
unsigned int version = hexToDec(header.substr(0,1));
if (version !=4 && version !=6){
std::cout << "Error: Wrong version (should be 4 or 6)"<< std::endl;
return 1;
}
unsigned int headerLength = hexToDec(header.substr(1,1))*4;
unsigned int totalLength = hexToDec(header.substr(2,4));
unsigned int dataSize = totalLength - headerLength;
unsigned int protocol = hexToDec(header.substr(10,2));
std::string sourceIP = hexIPToDec(header.substr(12,8));
std::string destIP = hexIPToDec(header.substr(20,8));
// Display information
std::cout << "Version: "<< version << std::endl;
std::cout << "Header Length: "<< headerLength << std::endl;
std::cout << "Total Length: "<< totalLength << std::endl;
std::cout << "Size of Data: "<< dataSize << std::endl;
std::cout << "Protocol: "<< protocol << std::endl;
std::cout << "Source IP address: "<< sourceIP << std::endl;
std::cout << "Destination IP address: "<< destIP << std::endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!