我如何在c中将一个非常大的十六进制数转换为十进制数组?

发布于 2025-01-15 01:32:24 字数 702 浏览 5 评论 0原文

我如何在c中将一个非常大的十六进制数转换为十进制数组?

unsigned char hexnr[32]={0xa5,0xe0,0x43,0xe4,0x10,0xb7,0x3a,0x7e,0xdf,0xba,0xec,0x78,0x52,0x82,0xfd,0xd2,0xea,0x43,0xec,0x53,0xdb,0x24,0xbc,0xdd,0xbb,0x5d,0x2c,0xc4,0x45,0x98,0xad,0x16};
                     
                    
char ResultInDecimals[]={7,5,0,2,7,8,6,2,3,2,4,3,8,2,7,5,9,7,4,8,5,4,6,3,7,0,0,5,2,4,2,8,7,7,3,5,3,6,8,3,3,3,8,0,8,1,9,0,5,7,1,1,6,5,1,9,1,5,2,0,5,5,4,7,0,7,8,7,6,2,5,7,9,0,7,4,2};

十六进制: 0xa5e043e410b73a7edfbaec785282fdd2ea43ec53db24bcddbb5d2cc44598ad16

十进制: 75027862324382759748546370052428773536833380819057116519152055470787625790742

how can i in c convert a very big hex number into an array of decimals?

unsigned char hexnr[32]={0xa5,0xe0,0x43,0xe4,0x10,0xb7,0x3a,0x7e,0xdf,0xba,0xec,0x78,0x52,0x82,0xfd,0xd2,0xea,0x43,0xec,0x53,0xdb,0x24,0xbc,0xdd,0xbb,0x5d,0x2c,0xc4,0x45,0x98,0xad,0x16};
                     
                    
char ResultInDecimals[]={7,5,0,2,7,8,6,2,3,2,4,3,8,2,7,5,9,7,4,8,5,4,6,3,7,0,0,5,2,4,2,8,7,7,3,5,3,6,8,3,3,3,8,0,8,1,9,0,5,7,1,1,6,5,1,9,1,5,2,0,5,5,4,7,0,7,8,7,6,2,5,7,9,0,7,4,2};

in hex : 0xa5e043e410b73a7edfbaec785282fdd2ea43ec53db24bcddbb5d2cc44598ad16

out decimal : 75027862324382759748546370052428773536833380819057116519152055470787625790742

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-22 01:32:24

基本运算是将 32 位 256 基数除以 10 并得到余数。

从结果中,我注意到“十六进制”数字和结果都首先存储最高有效数字。因此,除法可以实现如下:

#include<stdio.h>
#include<string.h>

// Divide 256-base number in "hex" by 10 and return remainder
unsigned char div10(unsigned char *hex, unsigned size)
{
    unsigned rem = 0;
    for(int i = 0; i < size; i++)
    {
        unsigned n = rem * 256 + hex[i];
        hex[i] = n / 10;
        rem = n % 10;
    }
    return rem;
}

将其应用于给定的示例可以如下完成:

unsigned char hexnr[32]={0xa5,0xe0,0x43,0xe4,0x10,0xb7,0x3a,0x7e,0xdf,0xba,0xec,0x78,0x52,0x82,0xfd,0xd2,0xea,0x43,0xec,0x53,0xdb,0x24,0xbc,0xdd,0xbb,0x5d,0x2c,0xc4,0x45,0x98,0xad,0x16};  // Most significant digit first

unsigned char result[80]={0};  // Fixed 80 digits

char ResultInDecimals[]={7,5,0,2,7,8,6,2,3,2,4,3,8,2,7,5,9,7,4,8,5,4,6,3,7,0,0,5,2,4,2,8,7,7,3,5,3,6,8,3,3,3,8,0,8,1,9,0,5,7,1,1,6,5,1,9,1,5,2,0,5,5,4,7,0,7,8,7,6,2,5,7,9,0,7,4,2};   // Known result for comparison


int main(void)
{
    unsigned char hexzero[32] = {0};
    unsigned i = 0;
    while(memcmp(hexnr, hexzero, sizeof(hexnr)) != 0 && i < sizeof(result))
    {
        result[sizeof(result) - i - 1] = div10(hexnr, sizeof(hexnr));
        i++;
    }

    if(memcmp(hexnr, hexzero, sizeof(hexnr)) != 0)
    {
        printf("ERROR: result buffer too small\n");
    }
    else
    {
        for(unsigned j = 0; j < sizeof(result); j++)
        {
            printf("%c", result[j]+'0');
        }
        printf("\n");
    }
}

打印结果:

00075027862324382759748546370052428773536833380819057116519152055470787625790742

The basic operation is to divide the 32-digit base 256 number with 10 and get the remainder.

From the result, I notice that both the "hex" number and the result are stored with the most significant digit first. Hence the division method can be implemented as follows:

#include<stdio.h>
#include<string.h>

// Divide 256-base number in "hex" by 10 and return remainder
unsigned char div10(unsigned char *hex, unsigned size)
{
    unsigned rem = 0;
    for(int i = 0; i < size; i++)
    {
        unsigned n = rem * 256 + hex[i];
        hex[i] = n / 10;
        rem = n % 10;
    }
    return rem;
}

Applying this to the given example can be done like this:

unsigned char hexnr[32]={0xa5,0xe0,0x43,0xe4,0x10,0xb7,0x3a,0x7e,0xdf,0xba,0xec,0x78,0x52,0x82,0xfd,0xd2,0xea,0x43,0xec,0x53,0xdb,0x24,0xbc,0xdd,0xbb,0x5d,0x2c,0xc4,0x45,0x98,0xad,0x16};  // Most significant digit first

unsigned char result[80]={0};  // Fixed 80 digits

char ResultInDecimals[]={7,5,0,2,7,8,6,2,3,2,4,3,8,2,7,5,9,7,4,8,5,4,6,3,7,0,0,5,2,4,2,8,7,7,3,5,3,6,8,3,3,3,8,0,8,1,9,0,5,7,1,1,6,5,1,9,1,5,2,0,5,5,4,7,0,7,8,7,6,2,5,7,9,0,7,4,2};   // Known result for comparison


int main(void)
{
    unsigned char hexzero[32] = {0};
    unsigned i = 0;
    while(memcmp(hexnr, hexzero, sizeof(hexnr)) != 0 && i < sizeof(result))
    {
        result[sizeof(result) - i - 1] = div10(hexnr, sizeof(hexnr));
        i++;
    }

    if(memcmp(hexnr, hexzero, sizeof(hexnr)) != 0)
    {
        printf("ERROR: result buffer too small\n");
    }
    else
    {
        for(unsigned j = 0; j < sizeof(result); j++)
        {
            printf("%c", result[j]+'0');
        }
        printf("\n");
    }
}

which prints the result:

00075027862324382759748546370052428773536833380819057116519152055470787625790742
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文