1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>

typedef unsigned char uchar;

void write_file(char const* filename, uchar const* contents) {
    FILE* fp = fopen(filename, "w");
    for(int i = 0; contents[i] != '\0'; i++) {
        uchar char_to_write = contents[i];
        fputc(char_to_write, fp);  // write one character
    }
    fclose(fp);
}

int main(int argc, char* argv[]) {
    uchar const contents[] = { '\xde', '\xca', '\xf0', 'B' };
    // '\xde' ⇒ 0xde ⇒ 11011110
    //                 └──┘└──┘
    //                  d   e
    //
    // '\xca' ⇒ 0xca ⇒ 11001010
    //                 └──┘└──┘
    //                  c   a
    //
    // '\xf0' ⇒ 0xf0 ⇒ 11110000
    //                 └──┘└──┘
    //                  f   0
    //
    // 'B' ⇒ 66    (might be helpful to remember that 'A' ↔ 65 ↔ 0x41
    //     ⇒ 0x42    ⇒ 01000010
    //                 └──┘└──┘
    //                  4   2
    write_file("quiz4.txt", contents);
    return EXIT_SUCCESS;
}

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.