c - 程序没有给出十六进制值?

标签 c math binary char hex

我的程序为 Decimal 提供了正确的值,但我使用 char 来 gt Hex 值

有人可以看到我的代码并告诉我为什么我的输出只是一个“?” (附上我的输出图像)

我知道您可以添加字符值。

这是我的代码。请不要试图改变我的整个代码,说我正在做的是“更长的路”。

注意,我现在只测试 HexVal1,因为它只考虑前 4 位。

程序应该做的是获取二进制的前 4 位并抛出一个十六进制字符。

#include <math.h>
#include <stdio.h>

int main()
{

    int bit1,bit2,bit3,bit4,bit5,bit6,bit7,bit8;
    int dec1,dec2,dec3,dec4,dec5,dec6,dec7,dec8;
    char hex1,hex2,hex3,hex4,hex5,hex6,hex7,hex8;
    int BinVal;
    char HexVal1;
    char HexVal2;

    printf("Please enter the first bit(0 or 1): ");
    scanf("%d",&bit1);
    printf("Please enter the second bi(0 or 1): ");
    scanf("%d",&bit2);  
    printf("Please enter the third bit(0 or 1): ");
    scanf("%d",&bit3);  
    printf("Please enter the fourth bit(0 or 1): ");
    scanf("%d",&bit4);  
    printf("Please enter the fifth bit(0 or 1): ");
    scanf("%d",&bit5);  
    printf("Please enter the sixth bit(0 or 1): ");
    scanf("%d",&bit6);  
    printf("Please enter the seventh bit(0 or 1): ");
    scanf("%d",&bit7);
    printf("Please enter the eigth bit:(0 or 1): ");
    scanf("%d",&bit8);


    if(bit1==1){
        dec1=1;
        hex1='1';
        }

    else{
    dec1=0;
    hex1='0';
    }

        if(bit2==1){
        dec2=2;
        hex2='2';
        }

    else{
    dec2=0;
    hex2='0';
    }

        if(bit3==1){
        dec3=4;
        hex3='4';
        }

    else{
    dec3=0;
    hex3='0';
    }

        if(bit4==1){
        dec4=8;
        hex4='8';
        }

    else{
    dec4=0;
    hex4='0';
    }

        if(bit5==1){
        dec5=16;
        hex5='16';
        }

    else{
    dec5=0;
    hex5='0';
    }

        if(bit6==1){
        dec6=32;
        hex6='32';
        }

    else{
    dec6=0;
    hex6='0';
    }

        if(bit7==1){
        dec7=64;
        hex7='64';
        }

    else{
    dec7=0;
    hex7='0';
    }

        if(bit8==1){
        dec8=128;
        hex8='128';
        }

    else{
    dec8=0;
    hex8='0';   
       }






    BinVal=dec1+dec2+dec3+dec4+dec5+dec6+dec7+dec8;
    printf("Binary value for your decimal number is %d",BinVal);

    HexVal1=sizeof(hex1)+sizeof(hex2)+sizeof(hex3)+sizeof(hex4);

    if(HexVal1==15){
        HexVal1='F';
    }

    else if (HexVal1==14){
     HexVal1='E';
    }

    else if (HexVal1==13){

    HexVal1='D';
    }

    else if(HexVal1==12){

    HexVal1='C';
    }

    else if(HexVal1==11){

    HexVal1='B';
    }

    else if(HexVal1==10){
    HexVal1='A';
    }


    printf("\nHex Val for first 4 bits is %c", HexVal1);

    return 0;

}

输出如下

Please enter the first bit(0 or 1): 1
Please enter the second bi(0 or 1): 0
Please enter the third bit(0 or 1): 0
Please enter the fourth bit(0 or 1): 1
Please enter the fifth bit(0 or 1): 1
Please enter the sixth bit(0 or 1): 1
Please enter the seventh bit(0 or 1): 1
Please enter the eigth bit:(0 or 1): 1
Binary value for your decimal number is 249
Hex Val for first 4 bits is 

最佳答案

无需重写整个内容。把“长远”的事情作为一种学习练习是很有值(value)的。您可以稍后进行优化。但是,由于一些误解,您的代码无法正常工作。看看这些主题:

因此,为了让代码正常运行,您根本不想将这些单引号值分配给“十六进制”变量;您只想分配不带引号的数值。 (此外,您至少应该使用 unsigned char 来确保它们可以容纳 128 的值。)

unsigned char hex1,... /* etc. */

hex8=128;

然后您可以将它们相加:

unsigned char HexVal1, HexVal2;
HexVal1 = hex1 + hex2 + hex3 + hex4;
HexVal2 = (hex5 + hex6 + hex7 + hex8) / 16;

这将为 HexVal1 和 HexVal2 提供您期望的 0 到 15 范围。 (与原始代码不同,其中 HexVal1 的值始终为 4!)

那么,你最后的转化也是有问题的。您将数字值 10-15 更改为字符“A”-“F”,这很好......但您也必须转换较低的值! 4'4' 不同。 (这就是您的程序输出问号的原因。HexVal1 包含值 4,它不是可打印字符。请参阅 ASCII character set 。)

您可以做很多很多事情来进一步改进,但如果您进行了这些最小的修复,您的代码应该可以工作。

关于c - 程序没有给出十六进制值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49340248/

相关文章:

c++ - 节拍器精度算法数学

binary - 从 stdin 接收二进制数据,发送到 Go 中的 channel

java - 移位时字节值的内存分配

algorithm - 计算二进制数的底数对数

c - 使用 ARM Cortex SysTick 计算运行时间;当 SysTick 计数器翻转时会发生什么?

c++ - 使用 librsvg 时出现编译错误

python - 欧拉问题21,为什么我的解中有多余的数字?

缓存 block 标记大小

C - 等待,获取返回值时失败

javascript - 从数组中随机选择图像