C语言int2str的一个 实现

最近项目中遇到一个问题,printf函数打印32位的整形变量的时候会出现问题,反复尝试修改print函数依然没有效果,只能另辟蹊径,将变量先转换成字符串数组在进行打印,下面就是一个简单的整形到字符串的转换函数:

#include "stdio.h"
// 定义结果缓冲区大小
#define BUFFER_LEN  (200)
char buffer[BUFFER_LEN + 1];

char* int2str(int val, int radix){
    char flag = 0;
    int index = 0;
    char b = 0;
    buffer[BUFFER_LEN] = 0;
    if(0 == val){
        buffer[BUFFER_LEN - 1] = '0';
        return (buffer + BUFFER_LEN - 1);
    }
    if (val < 0){
        flag = 1;
        val = -val;
    }
    /* 开始转换,注意防止缓冲区溢出 */
    while(index < BUFFER_LEN && val){
        /* 取个位数,实际可以直接%运算实现,但是这个不给力的编译器不支持%运算,
         * 也就只能通过如下的方式取出最低的个位数 */
        b = val - (val / radix * radix);
        // b = val % radix
        buffer[BUFFER_LEN - index -1] = b >= 10 ? ((b - 10) + 'A') : (b + '0')
        val = val / radix;
        index ++;
    }
    if (1 == flag){
        /* 根据需要添加符号位 */
        buffer[BUFFER_LEN - index -1] = '-';
        index ++;
    }
    /* 返回结果 */
    return (buffer + BUFFER_LEN - index);
}
/* 简单的测试函数 */
int main(){
    printf("Hello World!\r\n");
    for(int i = 0; i < 4000; i = i + 123){
        printf("%d = %s\t", i, int2str(i, 2));
        printf("%d = %s\t", i, int2str(i, 8));
        printf("%d = %s\t", i, int2str(i, 10));
        printf("%d = %s\r\n", i, int2str(i, 16));
    }
}

测试结果输出如下:

Hello World!
0 = 0    0 = 0    0 = 0    0 = 0
123 = 1111011    123 = 173    123 = 123    123 = 7B
246 = 11110110    246 = 366    246 = 246    246 = F6
369 = 101110001    369 = 561    369 = 369    369 = 171
492 = 111101100    492 = 754    492 = 492    492 = 1EC
615 = 1001100111    615 = 1147    615 = 615    615 = 267
738 = 1011100010    738 = 1342    738 = 738    738 = 2E2
861 = 1101011101    861 = 1535    861 = 861    861 = 35D
984 = 1111011000    984 = 1730    984 = 984    984 = 3D8
1107 = 10001010011    1107 = 2123    1107 = 1107    1107 = 453
1230 = 10011001110    1230 = 2316    1230 = 1230    1230 = 4CE
1353 = 10101001001    1353 = 2511    1353 = 1353    1353 = 549
1476 = 10111000100    1476 = 2704    1476 = 1476    1476 = 5C4
1599 = 11000111111    1599 = 3077    1599 = 1599    1599 = 63F
1722 = 11010111010    1722 = 3272    1722 = 1722    1722 = 6BA
1845 = 11100110101    1845 = 3465    1845 = 1845    1845 = 735
1968 = 11110110000    1968 = 3660    1968 = 1968    1968 = 7B0
2091 = 100000101011    2091 = 4053    2091 = 2091    2091 = 82B
2214 = 100010100110    2214 = 4246    2214 = 2214    2214 = 8A6
2337 = 100100100001    2337 = 4441    2337 = 2337    2337 = 921
2460 = 100110011100    2460 = 4634    2460 = 2460    2460 = 99C
2583 = 101000010111    2583 = 5027    2583 = 2583    2583 = A17
2706 = 101010010010    2706 = 5222    2706 = 2706    2706 = A92
2829 = 101100001101    2829 = 5415    2829 = 2829    2829 = B0D
2952 = 101110001000    2952 = 5610    2952 = 2952    2952 = B88
3075 = 110000000011    3075 = 6003    3075 = 3075    3075 = C03
3198 = 110001111110    3198 = 6176    3198 = 3198    3198 = C7E
3321 = 110011111001    3321 = 6371    3321 = 3321    3321 = CF9
3444 = 110101110100    3444 = 6564    3444 = 3444    3444 = D74
3567 = 110111101111    3567 = 6757    3567 = 3567    3567 = DEF
3690 = 111001101010    3690 = 7152    3690 = 3690    3690 = E6A
3813 = 111011100101    3813 = 7345    3813 = 3813    3813 = EE5
3936 = 111101100000    3936 = 7540    3936 = 3936    3936 = F60

一点说明

这里面进行转换的时候是从缓冲区末尾开始转换的,这样刚好可以适应我们的书写顺序,也不用后面再将顺序颠倒,运行效率也比较高。

发表新评论