CSU22014 Systems Programming Term 2022 p2
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CSU22014
Michaelmas Term 2022
Systems Programming
2. The original ASCII standard defined only 128 characters. Thus, each character could be specified using only 7 bits. The Pascal programming language provides a “packed” string type, which exploits the requirements of only 7 bits per character to pack strings of characters into a smaller space. For example,a string often characters (including the NULL terminating character) could fit into 9 bytes (i.e. 70 bits requires 8 bytes and 6 bits, which rounds up to 9 bytes).
Write two C functions as follows: The first should take a standard NULL-terminated C string, and return a newly created string which contains the packed representation of the
original string. The prototype of the function should be as follows:
unsigned char * string_to_packed(char * string); [50 marks]
The second function should take a packed string as a parameter, and should return a newly created string which contains the corresponding unpacked representation. The
prototype for this function should be as follows:
char * packed_to_string(const unsigned char * packed_string); [50 marks]
Your routines should be implemented in the related files in the assignment for the exam on Blackboard. Note that a string in C is an array of characters terminated by the ‘\0’
character. The packed string should also be terminated by a 7-bit packed ‘\0’ character.
Note that it is difficult to determine the length of a packed string without first unpacking it. To simplify unpacking, you may assume that the maximum string length is 1024 characters. This will allow you to unpack the packed string into a temporary array of 1024 characters, and then copy the string into a new array of a suitable size to return from the function.
2023-08-19