在四边形中将十六进制转换为二进制

时间:2017-05-23 19:28:33

标签: c string printf

该函数接收表示HEX编号的字符串(例如“12a”)。 应该通过包(每个4位)返回与字符串格式的双号相同的值,例如, 0001 0010 1010:

char* sixteen_to_two(char* hex)
{
char* r_stt = malloc(sizeof(char) * 5 * strlen(hex));
char* begin = r_stt;
do
{
    switch(*hex)
    {
        case '0':
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '0';
        break;

        case '1':
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '1';
        break;

        case '2':
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '0';
        break;

        case '3':
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '1';
        break;

        case '4':
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '0';
        break;

        case '5':
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '1';
        break;

        case '6':
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '0';
        break;

        case '7':
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '1';
        break;

        case '8':
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = ' ';
        break;

        case '9':
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '0';
        *r_stt++ = '1';

        break;

        case 'a':
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '0';
        break;

        case 'b':
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '1';
        *r_stt++ = '1';
        break;

        case 'c':
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '0';
        break;

        case 'd':
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '0';
        *r_stt++ = '1';
        break;

        case 'e':
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '0';
        break;

        case 'f':
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '1';
        *r_stt++ = '1';
        break;
    }
    *r_stt++ = ' ';
    hex++;
}
while(*hex != '\0');
return begin;

}

稍后,另一个函数想要扫描十进制数并通过执行以下操作以不同的数字格式将值控制给用户:

printf("= %d(8) = %d(10) = %s(16) = %s(2) \n", dec_to_eight(decimal),  decimal, dec_to_sixteen(decimal), sixteen_to_two(dec_to_sixteen(decimal)));

一切似乎都能正常工作,但是,对于生成恰好4位数的HEX的输入而言并非如此。看看会发生什么:

Look what happens in the picture

我不知道这个0 0 1 9的来源是什么,如何摆脱它?非常感谢。如果重要:这是GNOME Terminal 3.0.1

2 个答案:

答案 0 :(得分:2)

您的字符串未终止。将\0添加到字符串的末尾。

char* r_stt = malloc(sizeof(char) * 5 * strlen(hex) + 1);

// Other code

do
{
    // Loop code
} while(*hex != '\0');
*(--r_stt) = '\0'; // <-- Add this line.
return begin;

答案 1 :(得分:1)

正如改进一样,我会使用strcpy复制数组中的二进制表示。这可确保如果目标足够大以接收副本,结果将始终以null结尾:

char* sixteen_to_two(char* hex) {
    const char* binary[] = {
        "0000 ", "0001 ", "0010 ", "0011 ",
        "0100 ", "0101 ", "0110 ", "0111 ",
        "1000 ", "1001 ", "1010 ", "1011 ",
        "1100 ", "1101 ", "1110 ", "1111 "
    };

    char* r_stt = malloc(sizeof(char) * 5 * strlen(hex) + 1);
    char* begin = r_stt;

    while(*hex != '\0') {
        int bin_index;

        /* create index from current hexadecimal character */
        if(*hex >= '0' && *hex <= '9')
            bin_index = *hex - '0';
        else if(*hex >= 'A' && *hex <= 'F')
            bin_index = (*hex - 'A') + 10;
        else if(*hex >= 'a' && *hex <= 'f')
            bin_index = (*hex - 'a') + 10;
        else
            break;

        /* copy the binary representation and adjust pointers */
        strcpy(r_stt, binary[bin_index]);
        r_stt += 5;
        hex++;
    }

    return begin;
}