diff options
author | Alexis Hovorka <[email protected]> | 2024-08-17 19:32:33 -0600 |
---|---|---|
committer | Alexis Hovorka <[email protected]> | 2024-08-17 19:32:33 -0600 |
commit | 3712c0af48b9cc68fe52729cb7aa24006e35cb5d (patch) | |
tree | 14ebf05a0335624d56b171c7f17450d0baa339bb /static/20240817-aarch64-bootstrapping-1/hex.c | |
parent | d2a685e50d20101a42b7d9d08d1b90bb92defcd3 (diff) |
[post] AArch64 Bootstrapping 1
Diffstat (limited to 'static/20240817-aarch64-bootstrapping-1/hex.c')
-rw-r--r-- | static/20240817-aarch64-bootstrapping-1/hex.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/static/20240817-aarch64-bootstrapping-1/hex.c b/static/20240817-aarch64-bootstrapping-1/hex.c new file mode 100644 index 0000000..aca16e2 --- /dev/null +++ b/static/20240817-aarch64-bootstrapping-1/hex.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +int main(void) { + char c = 0; // Input character + int count = 0; // Hex digits parsed for this number + long long v = 0; // Current value + + while ((c = getchar()) != EOF) { + + // Skip comments + if (c == '\\') { while ((c = getchar()) != EOF && c != '\n') {} } + + // Process incoming digits and add to running value + else if (('0' <= c) && (c <= '9')) { v = (v << 4) + (c - '0'); count++; } + else if (('a' <= c) && (c <= 'f')) { v = (v << 4) + (c - 'a' + 10); count++; } + + // Send the finished number out the other end + else if (c == ' ') { + + // Turn the number of hex digits into a number of bytes + if (count & 1) count++; + count >>= 1; + + // Emit each byte, littlest first + for (; count>0; count--) { + c = v & 0xFF; + putchar(c); + v >>= 8; + } + } + } +} |