# 其他 - 如何在C语言中巧妙地将32位以上的位数（38位）转换为32位系统上的字符串

The question is complete "as is", there is no need to look for the perceived true intention of the question elsewhere.

Horst

16
r3mainer 2020-01-31 20:36

This ought to work. It uses a static array to store the results, so you'll need to arrange for these to be copied elsewhere if you don't want them to be clobbered by the next call.

``````#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define BIG_WORD_LENGTH 5

// Length of output is at most ceil(BIG_WORD_LENGTH * log(256) / log(10))
// The following is a slight overestimate, but close enough
#define BIG_WORD_STRLEN (3 + (BIG_WORD_LENGTH) * 5 / 2)

typedef uint8_t big_word[BIG_WORD_LENGTH];

char *big_word_2_str(big_word num) {
// Make a local copy of the number
big_word n;
for (int i=0; i<BIG_WORD_LENGTH; i++) {
n[i] = num[i];
}

// Result goes here
static char result[BIG_WORD_STRLEN];
int p = BIG_WORD_STRLEN-1;
result[p--] = '\0';

// Calculate digits in base 10
for (int i=1; i<BIG_WORD_STRLEN; i++) {
int x, tmp = 0;
for (int j=0; j<BIG_WORD_LENGTH; j++) {
x = n[j];
x += tmp << 8;
tmp = x % 10;
n[j] = x / 10;
}
result[p--] = '0' + tmp;
}

while (++p < BIG_WORD_STRLEN-2 && result[p] == '0');

return result + p;
}

// Test:
int main() {
// 0x492559f64f = 314159265359
big_word x = { 0x49, 0x25, 0x59, 0xf6, 0x4f };
puts(big_word_2_str(x));
return 0;
}
``````