#include "helper.h" // Returns the n'th bit of a uint32_t value. uint32_t get_nth_bit(const uint32_t value, const int n) { return (value & (1u << n)) >> n; } // Prints the bits of a uint32_t value. void print_bits(const uint32_t value) { for (int i = (int)sizeof(uint32_t)*8-1; i >= 0; --i) { printf("%d", get_nth_bit(value, i)); } printf("\n"); } // Skips past the next newline. // If EOF, stays on EOF so that subsequent fgetc() calls return EOF. void skip_past_newline(FILE *const fptr) { int c = 0; c = fgetc(fptr); while (true) { if (c == EOF) { fseek(fptr, -1L, SEEK_CUR); return; } else if (c == '\n') { printf("newline\n"); return; } else { fseek(fptr, -1L, SEEK_CUR); return; } printf("skipping char\n"); c = fgetc(fptr); } } // Converts decimal values to hex obviously. uint32_t hex_to_decimal(const uint32_t decimal) { uint32_t ret = 0; for (size_t i = 0; i < sizeof(uint32_t); ++i) { // Iterate through 4 bytes. uint32_t tmp = (decimal & (0xFFu << 8*i)) >> 8*i; if (isalpha(tmp)) { tmp -= 'a'; tmp += 10; } else if (isdigit(tmp)) { tmp -= '0'; } // There should be no other cases if the input is hex. ret |= (tmp << 4*i); } return ret; }