From 99cbca6f88bd88adead3ee77a485480d2c885150 Mon Sep 17 00:00:00 2001 From: feilong Date: Sat, 23 Oct 2021 21:08:20 +0800 Subject: [PATCH] add hex --- .../bin_to_hex.c" | 22 +++++ .../bin_to_hex.json" | 5 ++ .../bin_to_hex.md" | 80 +++++++++++++++++++ .../config.json" | 4 +- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.c" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.json" create mode 100644 "data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.md" diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.c" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.c" new file mode 100644 index 0000000..53780df --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.c" @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +void bin_to_hex(const void *data, uint32_t len, char *out) { + static const char *const lut = "0123456789abcdef"; + uint32_t i; + for (i = 0; i < len; ++i) { + uint8_t c = ((uint8_t *)data)[i]; + out[i * 2] = lut[c >> 4]; + out[i * 2 + 1] = lut[c & 15]; + } +} + +int main(int argc, char **argv) { + char *str = "HelloWorld!"; + char *hex = (char *)malloc(512 * sizeof(char)); + bin_to_hex(str, strlen(str), hex); + printf("%s", hex); + return 0; +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.json" new file mode 100644 index 0000000..a6ff0f5 --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "幻灰龙", + "source": "bin_to_hex.md" +} \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.md" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.md" new file mode 100644 index 0000000..ec98e1b --- /dev/null +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/bin_to_hex.md" @@ -0,0 +1,80 @@ +# 十六进制转换 + +将任意二进制数据转成16进制并打印hex字符串 + +## template + +```c +#include +#include +#include +#include + +$code + +int main(int argc, char **argv) { + char *str = "HelloWorld!"; + char *hex = (char *)malloc(512 * sizeof(char)); + bin_to_hex(str, strlen(str), hex); + printf("%s", hex); + return 0; +} +``` + +## 答案 + +```c +void bin_to_hex(const void *data, uint32_t len, char *out) { + static const char *const lut = "0123456789abcdef"; + uint32_t i; + for (i = 0; i < len; ++i) { + uint8_t c = ((uint8_t *)data)[i]; + out[i * 2] = lut[c >> 4]; + out[i * 2 + 1] = lut[c & 15]; + } +} +``` + +## 选项 + +### 选项1 + +```c +void bin_to_hex(const void *data, uint32_t len, char *out) { + static const char *const lut = "0123456789abcdef"; + uint32_t i; + for (i = 0; i < len; ++i) { + uint8_t c = ((uint8_t *)data)[i]; + out[i * 2] = lut[c >> 4]; + out[i * 2 + 1] = lut[c << 4]; + } +} +``` + +### 选项2 + +```c +void bin_to_hex(const void *data, uint32_t len, char *out) { + static const char *const lut = "0123456789abcdef"; + uint32_t i; + for (i = 0; i < len; ++i) { + uint8_t c = ((uint8_t *)data)[i]; + out[i * 2] = lut[c << 4]; + out[i * 2 + 1] = lut[c & 15]; + } +} +``` + +### 选项3 + +```c +void bin_to_hex(const void *data, uint32_t len, char *out) { + static const char *const lut = "0123456789abcdef"; + uint32_t i; + for (i = 0; i < len; ++i) { + uint8_t c = ((uint8_t *)data)[i]; + out[i * 2] = lut[c >> 4]; + out[i * 2 + 1] = lut[c & 16]; + } +} +``` \ No newline at end of file diff --git "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/config.json" "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/config.json" index 201aa80..8358476 100644 --- "a/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/config.json" +++ "b/data/3.C\350\257\255\350\250\200\351\253\230\351\230\266/3.\344\275\215\350\277\220\347\256\227/4.\345\215\201\345\205\255\350\277\233\345\210\266/config.json" @@ -6,5 +6,7 @@ "C语言" ], "children": [], - "export": [] + "export": [ + "bin_to_hex.json" + ] } \ No newline at end of file -- GitLab