在 C 中将十六进制转换为二进制 [英] Convert Hex to Binary in C

查看:21
本文介绍了在 C 中将十六进制转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将长十六进制字符串转换为 int 数组sscanf

我以前看过一些关于此的主题,但大多数解决方案都是用 Python 编写的(诅咒你这么容易!).

I have seen some topics on this before, but most solutions are in Python (curse you for being so easy!).

在 C 中是否有一种简单的方法可以做到这一点?这就是我的解决方案所需要的.我知道它在概念上并不复杂,但我对 C 字符串操作和地址处理感到非常恐惧.

Is there an easy way to do this in C? This is what my solution needs to be in. I know it's not complicated conceptually, but I am horrendous with C string manipulation and address handling.

谢谢你们的帮助,伙计们!

Thanks for the help, guys!

---------编辑-------------

----------EDIT-------------

好的,所以我解决了这个问题,我还有另一个问题(我不想打开一个新线程!).下面是我对给定代码的修改,包括函数调用和输出.

Okay, so I solved that problem, and I have another question (I didn't want to open up a new thread!). Below is my modification to the given code, with the function call and the output.

void htoi(const char *ptr, char *binAddr) {
char value[32] = "";
char ch = *ptr;
int i;
const char* quads[] = {"0000", "0001", "0010", "0011", "0100", "0101",
                     "0110", "0111", "1000", "1001", "1010", "1011",
                     "1100", "1101", "1110", "1111"};

while (ch == ' ' || ch == '	')
    ch = *(++ptr);

for (i = 0; i < 8; i++) {
    if (ch >= '0' && ch <= '9')
        strncat(value, quads[ch - '0'], 4);
    if (ch >= 'A' && ch <= 'F')
        strncat(value, quads[10 + ch - 'A'], 4);
    if (ch >= 'a' && ch <= 'f')
        strncat(value, quads[10 + ch - 'a'], 4);

    ch = *(++ptr);
    printf("%s
", value);
}

*binAddr = *value;
}

这是我的函数调用:

char line[11], hexAddr[8], binAddr[32];
htoi(hexAddr, binAddr);
printf("%s
", binAddr);

这是输出(当输入 001133c0 时):

Here is the output (when input with 001133c0):

0000

00000000

000000000001

000000000001

0000000000010001

0000000000010001

00000000000100010011

00000000000100010011

000000000001000100110011

000000000001000100110011

0000000000010001001100111100

0000000000010001001100111100

00000000000100010011001111000000

00000000000100010011001111000000

0...

最后一行(带有特殊字符)是上面 main 函数中的 printf(binAddr).从函数内部的 printf 语句可以清楚地看出,二进制代码的构造是正确的.

The last line (with the special characters) is the printf(binAddr) in the main function above. It is clear from the printf statements inside the function that the binary code is being constructed correctly.

再一次,这归结为我在地址操纵方面一文不值.我做错了什么?

Once again, this comes down to me not being worth anything with address manipulation. What am I doing wrong?

推荐答案

使用查找表相当简单:

const char * const quads = { "0000", "0001", "0010", .... };

const char * hex_to_bin_quad(unsigned char c)
{
  if (c >= '0' && c <= '9') return quads[     c - '0'];
  if (c >= 'A' && c <= 'F') return quads[10 + c - 'A'];
  if (c >= 'a' && c <= 'f') return quads[10 + c - 'a'];
  return -1;
}

现在遍历您的字符串并为每个字符附加 hex_to_bin_quad(c)(例如,使用 strncat).你已经知道你需要一个长度为 4 * strlen(src) + 1 的目标字符串,所以分配、迭代和连接.

Now iterate over your string and append hex_to_bin_quad(c) for each character (e.g. using strncat). You already know that you'll need a target string of length 4 * strlen(src) + 1, so allocate, iterate and concatenate.

这篇关于在 C 中将十六进制转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆