-
Notifications
You must be signed in to change notification settings - Fork 0
/
printSpacedHexBin.c
48 lines (38 loc) · 941 Bytes
/
printSpacedHexBin.c
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
// Copyright 2018 (c), Neven Sajko. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the
// LICENSE file.
#include <stdio.h>
#include <string.h>
#include "buffer/buffer.h"
#include "encoding/encoding.h"
void
f(const char *s) {
size_t i;
buffer hexBuf, *decodedBuf, *binaryBuf;
hexBuf.l = strlen(s);
hexBuf.b = s;
decodedBuf = hexDecodeAlloc(&hexBuf);
binaryBuf = binaryEncodeAlloc(decodedBuf);
for (i = 0; i < decodedBuf->l; i++) {
fprintf(stdout, "%8lu ", i);
}
fprintf(stdout, "\n");
for (i = 0; i < decodedBuf->l; i++) {
fprintf(stdout, "%8.2s ", &(hexBuf.b[i * 2]));
}
fprintf(stdout, "\n");
for (i = 0; i < decodedBuf->l; i++) {
fprintf(stdout, "%8.8s ", &(binaryBuf->b[i * 8]));
}
fprintf(stdout, "\n");
bufferFree(decodedBuf);
bufferFree(binaryBuf);
}
int
main(int argc, char *argv[]) {
if (argc != 2) {
return 1;
}
f(argv[1]);
return 0;
}