CS253: Software Development with C++

Spring 2018

HW 1

CS253 HW1: Serialization                


For this assignment, you will write a C++ program called hw1 that reads integers from standard input, translates them to a serialized format, and writes the two-digit hexadecimal form of the serialized numbers to standard output, one per line.                 

Definition of a vnum                

We define a vnum, or variable-length number, as a two’s complement, variable number of bits (but always of the length 8n+4, where n≥0), prefixed by a four-bit length. The output will always be a whole number of bytes, or a multiple of eight bits.                 

You can consider the length nibble to be either:

For example, the number five, in binary, is 1012. We need to pad that to a length of 8n+4. Since it’s three bits long, we pad it on the left to four bits: 01012. This requires no additional bytes, so the four-bit length is zero, or 00002. Put those together: 0000 01012, or 0516.                 

The number three hundred, in binary, is 1 0010 11002. Padded to length 8n+4, that is 0001 0010 11002. This requires two bytes, or one additional byte, so the four-bit length is one, or 00012. Put those together: 0001 0001 0010 11002, or 112c16.                 

The number negative one, in two’s complement binary, is 11112. This requires no additional bytes, so the four-bit length is zero, or 00002. Put those together: 0000 11112, or 0f16.                 

A vnum must always be the represented using the minimum number of bytes. For example, seven must serialize as 0716, not as 30 00 00 0716.                 

A one-byte vnum (-8 ≤ n < 8)
byte #1
length bits
0 0 0 0 b3 b2 b1 b0
A two-byte vnum
byte #1 byte #2
length bits
0 0 0 1 b11 b10 b9 b8 b7 b7 b5 b4 b3 b2 b1 b0
A three-byte vnum
byte #1 byte #2 byte #3
length bits
0 0 1 0 b19 b18 b17 b16 b15 b14 b13 b12 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

In these examples, b0 is the least significant bit (LSB), b1 is next most significant bit, etc.                 

The four-bit length field has a maximum value of 15, so this scheme can only store a 16-byte, or 124-bit number.                 

Sample Runs                

Here are several sample runs, where % is my prompt. What you type looks like this. Your output must match exactly. Ignore the indentation.                 

    % make
    make output appears here
    % echo '5 300 -1 1 2 -3 40 500' | ./hw1
    5: 05
    300: 11 2c
    -1: 0f
    1: 01
    2: 02
    -3: 0d
    40: 10 28
    500: 11 f4
    % ./hw1
    -6000: 2f e8 90
    70000: 21 11 70
    1000000000: 40 3b 9a ca 00

How to emit hex                

// Emit two-digit hexadecimal.
int n = 200;
cout << "decimal: " << dec << n << '\n'
     << "hex: " << hex << setw(2) << setfill('0') << (n & 0xff) << '\n';
decimal: 200
hex: c8


If you have any questions about the requirements, ask. In the real world, your programming tasks will almost always be vague and incompletely specified. Same here.                 

Tar file                

How to submit your homework:                

    ~cs253/bin/checkin HW1 hw1.tar

How to receive negative points:                

Turn in someone else’s work.                 

User: Guest                 

Edit History Source

Modified: 2018-04-02T17:06                 

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building