Lab 6

Lab description

Write a program C++ with two options that are the inverse of each other: Packing 4 characters into an unsigned long int. The 4 characters should be entered individually and the result should be content of the long variable. Unpack an unsigned long int into 4 characters. A single long value is entered and this should be parsed into 4 characters and these be displayed.


#include 
#include 
#include 

using namespace std;

long PackChars(char c[4])
{
	long r = 0;
	for(int i = 0; i < 4; ++i)
	{
		r = r | (long)c[i];
		if(i != 3)
			r = r << 8;
	}
	return r;
}

void UnpackLong(long t)
{
	for(int i = 0; i < 4; ++i)
	{
		cout << (char)(t & 0x000000FF) <> 8);
	}
	cout << endl;
}

int main(int argc, char* argv[])
{

	char c[4];
	while(1)
	{
		cout << "Enter 4 characters for packed into a long int:" << endl;
		for(int i = 0; i > c[i];
		}
		cout << "Packed long: " << PackChars(c) << endl;

		cout << " Enter a long value for being unpacked into 4 chars" <> t;
		UnpackLong(t);
	}
	return 0;
}

hi

Leave a Reply

Your email address will not be published. Required fields are marked *