blob: ebe798ba20c6b5d27e655515ef17c1ea360b85a1 (
plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#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;
}
|