简体   繁体   English

为什么我的 char 数组没有在 C 中打印出来?

[英]Why isn't my char array printing out in C?

I have this program that is trying to convert a character string to a different base without using atoi.我有这个程序试图在不使用 atoi 的情况下将字符串转换为不同的基数。 Why isn't the output printing at the end?为什么最后没有打印 output? The text "Output:" prints, but there is nothing after it.文本“输出:”打印出来,但后面什么都没有。

The code seems to work up until that point.代码似乎一直有效。 I went ahead and added the null byte.我继续添加了 null 字节。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void Int(char* numstring, int base) {

    char *digitPtr = numstring;
    char ans[100];

    int decVal = 0;
    int totalVal = 0;

    int numDigits = 0;

    // Go through the entire string to get the decimal value of the number.

    while (*digitPtr != '\0') {
        numDigits++;
        digitPtr++;
    }

    // Now calculate decimal value of the string.

    printf("before decimal value calc \n");
    //  *digitPtr = numstring;

    int length = numDigits;

    numDigits--;

    printf("numDigits: %d \n", numDigits);

    
    for (int i = 0; i < length; i++)
    {
        printf("calcuating the dec \n");
        decVal = (*numstring - 48);
        printf("dec Value: %d \n", decVal);
        printf("power: %f \n", pow(10, numDigits));     

        totalVal = totalVal + decVal *  pow(10, numDigits);

        printf("totalVal %d \n", totalVal);
        numDigits--;
        numstring++; 
    }

    //*digitPtr = numstring;
    numDigits = length;

    printf("totalVal %d \n", totalVal);

    while (totalVal > 0) {
        int remainder = totalVal % base;
        printf("remainder: %d \n", remainder);
        totalVal = totalVal / base;
        ans[numDigits] = remainder;
        numDigits--;
    }

    ans[99] = '\0'; 

    printf("Output: %s \n", ans); 
}

int main() {

It doesn't print anything because you're assigning the numerical value of the remainder to a char , but you're not considering that numbers start at 48 (that is, '0' ) in the ASCII table, so you should add the "starting point" to your remainder:它不会打印任何内容,因为您将余数的数值分配给char ,但您没有考虑到 ASCII 表中的数字从 48 (即'0' )开始,因此您应该添加“起点”到您的剩余部分:

ans[numDigits] = remainder + '0';

That said, there's another problem: you're assigning the length of the input value to numDigits before the while loop, but the length of the number won't probably be the same as length of the number in the new base (for example, 32 in base 10 is two digits long, but in base 2 it has four more digits).也就是说,还有另一个问题:您在while循环之前将输入值的长度分配给numDigits ,但数字的长度可能与新基数中的数字长度不同(例如,以 10 为底的 32 有两位长,但以 2 为底的还有四位)。 A solution could be to make numDigits start at 0, increment it after every iteration and print the string character by character in reverse order.一种解决方案可能是使numDigits从 0 开始,在每次迭代后递增,并以相反的顺序逐个字符地打印字符串。 Alternatively a mathematical approach for taking the number of digits of a number in a custom base is adding one to the rounded-down logarithm of the number in the custom base.或者,一种用于获取自定义基数中数字位数的数学方法是将自定义基数中的数字的四舍五入对数加一。 To do so you can you the properties of logarithm since C standard libraries only provide logs in base 2, e, and 10.为此,您可以使用对数的属性,因为 C 标准库仅提供以 2、e 和 10 为底的日志。

numDigits = (int)(floor(log(totalVal) / log(base) + 1);

Following this approach you don't have to change your loop and you can print the result as a string.按照这种方法,您不必更改循环,并且可以将结果打印为字符串。

Note that you shouldn't set ans[99] to '\0';请注意,您不应将ans[99]设置为'\0'; since there could be garbage between the converted number and the end of the string;因为转换后的数字和字符串结尾之间可能存在垃圾; instead, you should set it to numDigits .相反,您应该将其设置为numDigits You could also initialize ans to 0 (which is the numerical representation of \0 ), so you can forget about manually setting the terminator.您还可以将ans初始化为0 (这是\0的数字表示),因此您可以忘记手动设置终止符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM