Lab Description:
Lab 6 demonstrates the process of the packing and unpacking data. You will be given the choice to pack and unpack data. If you choose to pack data, you will be asked to input 4 chars( or letters). Then the program will translate these chars into hex and output the hex values of each letter next to each other. For example for ABCD, it will output 41424344. The output of the hex values is based on the ASCII chart which includes the value of a char in hex, decimal, octal, and html. If you would like to unpack data, in the menu you would press 2 and be taken to the unpacking menu where you will be ask to enter 4 long value in hex (you can get these values from the ASCII chart just mentioned). After inputing the 4 long values in hex, the program will then output its equivalent char.
Notes:
Modern day computers run on little endian processing, so the lowest order bit is placed first and the highest order bit is placed last. This will affect unpacking data in the program as if you were to input 41 42 43 44, it will output ‘DCBA’. If you would like to get ‘ABCD’ as output then input the reverse, 44 43 42 41.
Code:
#include using namespace std; int main() { int decision, option; char in, first, second, third, fourth; unsigned long int w, x, y, z; cout<<"Would you like to pack or unpack. Exit (3)."; //Note comments in program are if you were to you to enter ABCD or 41424344 into the program. cin>>decision; while(decision < 3) { if ( decision == 1) { cout<>hex>>first>>second>>third>>fourth; //x is input w = first; x = second; y = third; z = fourth; cout<<"Packed chars: "<<hex<<w; //41 cout<<x; //42 cout<<y; //43 cout<<z; //44 } else if (decision == 2) { cout<<"Please note that the modern computer using little endian processing so the lowest order bit will be printed first, then followed by the next lower order bit until the highest order bit is printed"<<endl; cout<<endl; cout<<"For example, if you were to input 41 42 43 44, output would be DCBA. If you want for it to output ABCD, reverse the order of the numbers and input 44 43 42 41, and the output will be ABCD"<<endl; cout<<endl; cout<<"Enter a long value in hex:"<>hex>>x; cout<<"Your chars are:"<>= 8; cout<>= 8; cout<>= 8; cout<<char(x); // A } else { cout<<"Exiting now."; break; } cout<<endl; cout<<"Would you like to pack or unpack. Exit (3)."; //Note comments in program are if you were to you to enter ABCD or 41424344 into the program. cin>>decision; } }