summaryrefslogtreecommitdiff
path: root/src/bin/misc
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2017-04-20 16:49:35 +0300
committerJari Vetoniemi <mailroxas@gmail.com>2017-05-01 22:58:22 +0300
commit29086b1d12a2c28cffdbfbf0b3990a7bd75506b9 (patch)
tree8acd48bc30932812744c0adb102d7a7add494357 /src/bin/misc
parent76b8c9e03c97b16d9ff97f3b79c0ecbff0f5e7f2 (diff)
work in progress
Diffstat (limited to 'src/bin/misc')
-rw-r--r--src/bin/misc/dec2bin.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/bin/misc/dec2bin.c b/src/bin/misc/dec2bin.c
new file mode 100644
index 0000000..be1dd5e
--- /dev/null
+++ b/src/bin/misc/dec2bin.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <err.h>
+
+int
+main(int argc, char *argv[])
+{
+ if (argc < 3)
+ errx(EXIT_FAILURE, "usage: %s <u8|u16|u32|u64> number", argv[0]);
+
+ const struct {
+ const char *t;
+ size_t sz;
+ } map[] = {
+ { .t = "u8", .sz = sizeof(uint8_t) },
+ { .t = "u16", .sz = sizeof(uint16_t) },
+ { .t = "u32", .sz = sizeof(uint32_t) },
+ { .t = "u64", .sz = sizeof(uint64_t) },
+ };
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+
+ size_t sz = 0;
+ for (size_t i = 0; i < ARRAY_SIZE(map); ++i) {
+ if (strcmp(argv[1], map[i].t))
+ continue;
+
+ sz = map[i].sz;
+ break;
+ }
+
+ if (!sz)
+ errx(EXIT_FAILURE, "unknown type: %s", argv[1]);
+
+ const uint64_t v = strtoll(argv[2], NULL, 10);
+ fwrite(&v, sz, 1, stdout);
+ return EXIT_SUCCESS;
+}