c语言如何用指针将十六进制字符串转换成二进制字符串?
1条回答 默认 最新
- 神仙404 2020-11-14 18:14关注
#include <stdio.h> #include <string.h> #include <malloc.h> #include <ctype.h> char x[17][5] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111", }; int index_of_x(char c) { if (isdigit(c)) { return c - '0'; } else if (isalpha(c)) { c = tolower(c); return (c - 'a') + 10; } } char* hex_to_bin(const char *hex_str) { int len = strlen(hex_str); char *bin_str = (char*)malloc(len * 4 + 1); const char *p = hex_str; char *q = bin_str; while (*p != '\0') { int idx = index_of_x(*p); memmove(q, x[idx], strlen(x[idx])); q += 4; p++; } *q = '\0'; return bin_str; } int main() { const char hex_str[] = "ab14cb"; char *dest = hex_to_bin(hex_str); printf("bin_str: %s\n", dest); free(dest); return 0; }
抛砖引玉, 多多交流。
1 个 16 进制数对应 4 位二进制,把握住这点就可以了
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效
- ¥15 悬赏!微信开发者工具报错,求帮改