十六进制转换成十进制(short、int)补码转换

#include 
#include 
void test01(unsigned char num1, unsigned char num2)//十六进制转十进制(2字节)
{
    short a,b,num;
    a = (num1^0xff) << 8;
    b = num2^0xff;
    num = -(a+b+1);
    printf("十六进制转十进制(short型): %d\n", num);
}

void test02(unsigned char num1, unsigned char num2, unsigned char num3, unsigned char num4)//十六进制转十进制(4字节)
{
    int a,b,c,d,num;
    a = ((num1)^0xff) << 24;
    b = ((num2)^0xff) << 16;
    c = ((num3 )^0xff) << 8;
    d = num4 ^0xff;
    num = -(a + b +c + d +1);
    printf("十六进制转十进制(int型): %d\n", num);
}

int main() {

    test01(0xff,0xd6);
    test02(0xff,0xff,0xf8,0x30);

    printf("Hello, World!\n");
    return 0;
}

你可能感兴趣的:(c语言)