将十六进制字符串转换为十进制整数,反之亦然

问题描述 投票:0回答:1

这里的十六进制系统有点不同,其中 A、B .... F 被 U-Z 替换。

#include<stdio.h>

int main() {

    // Write your program logic here. This line is a comment, you can leave it in code if you wish.
    // conversion of hexadecimal to decimal.
    
    //1.taking user input of string
    char hexaDec[10];
    int counter = 0;
    for(int i = 0; i<10; i++){
        scanf("%c", &hexaDec[i]);
        if(i<=8){
            if(hexaDec[i] == '\n'){
                hexaDec[i+1] = '\0';
                break;
            }
        }
        counter++;
    }
    // hexaDec[10] = '\0'; wasn't sure if this was required
    
    int dnum = 0;
    scanf("%d", &dnum);
    
    int decimalC = 0;
    int product = 1;
    while(counter>0){
        char cnum = hexaDec[counter-1];
        
        int check = cnum - 'U';
        if(cnum - '0' >= 0 && cnum - '0' <= 9){
            decimalC = decimalC + (cnum - '0') * product;
        }
        else if(check >=0 && check <= 5) {
            decimalC = decimalC + (check + 10)*product;
        }
        
        product = product*16;
        counter--;
    
    }
    printf("%d\n", decimalC);
    
    // //decimal to hexadecimal
    
    char decimalH[10];
    int count = 0;
    if(dnum>0){
        for(count =0; dnum>0  && count<=10; count++){
            int rem = dnum%16;
            dnum = dnum/16;
            if(rem>=0 && rem<=9){
                decimalH[count] = rem +'0';
            }
            else if(rem>=10 && rem<=15){
                decimalH[count] = rem -10 + 'U';
            }
        }        
    }
    
    else if(dnum == 0){
        printf("%c", '0');
    }
    
    while(count>0){
        printf("%c", decimalH[count-1]);
        count--;
    }
    printf("%c", '\0');
    return 0;
}

我知道代码真的很长,可以使用函数来简化,但我仍然想用原始方法尝试一下。似乎所有问题都没有问题,但大学网页仍然告诉我,我失败了一些案例。我已经尽力了。非常感谢任何帮助。预先感谢您。

arrays c string
1个回答
0
投票

您可以通过将数字存储在字符串中来转换为任何表示数字的字符。

char *mystrchr(const char *str, int ch)
{
    char *result = NULL;

    while(*str && *str != ch) str++;
    return *str ? (char *)str : NULL;
}

const char digits[]="0123456789UWVXYZ";

char *tostrrecursive(char *buff, unsigned long long int val)
{
    if(val) buff = tostrrecursive(buff, val / 16);
    else 
    {
        *buff = 0;
        return buff;
    }
    *buff = digits[val % 16];
    return buff+1;
}

char *tostr(char *buff, unsigned long long int val)
{
    if(val) return tostrrecursive(buff, val);
    else
    {
        buff[0] = digits[0];
        buff[1] = 0;
    }
    return buff;
}

unsigned long long toint(const char *hexstr)
{
    unsigned long long result = 0;
    while(*hexstr)
    {
        char *found = mystrchr(digits, *hexstr++);
        result *= 16;
        result += found ? found - digits : 0;
    }
    return result;
}

int main(void)
{
    char x[100];
    unsigned long long val;

    tostr(x, 0x1a2b3c4d5e6f);
    printf("%s\n", x);
    val = toint(x);
    printf("%llx\n", val);
}

然后您可以使用任何字符作为数字。

© www.soinside.com 2019 - 2024. All rights reserved.