C语言 将整数值存储在 C 中的字符数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15308783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:38:31  来源:igfitidea点击:

Store an integer value in a character array in C

ccharint

提问by Ayse

It is a very trivial question but I don't know why I am not getting the correct output. Here is what I am trying to do:

这是一个非常微不足道的问题,但我不知道为什么我没有得到正确的输出。这是我想要做的:

char sendBuffer[1000];
int count=0:
while(count<10)
{
    sendBuffer[0]=count;
    sendBuffer[1]='a';
    sendBuffer[2]='b';
    sendBuffer[3]='c';
    sendBuffer[4]='
sendBuffer[0] = '0' + count;
'; printf(%s,sendBuffer); count=count+1; }

In the output, all buffer is printed correctly except the first index. I want 1,2,3 and so on to be printed at the start of the buffer but it does not work. Please Help

在输出中,除了第一个索引外,所有缓冲区都被正确打印。我想在缓冲区的开头打印 1,2,3 等等,但它不起作用。请帮忙

回答by cnicutar

You need to convert that number into a character. A cheap way to do it is:

您需要将该数字转换为字符。一种廉价的方法是:

#define ENOUGH ((CHAR_BIT * sizeof(int) - 1) / 3 + 2)

char str[ENOUGH];
snprint(str, sizeof str, "%d", 42);

is there anyway to display integers greater than 9

无论如何要显示大于 9 的整数

If you need that you'll want to shift to more elaborate schemes. For example, if you want to convert an integer 42 into the string "42" you can say:

如果你需要,你会想要转向更复杂的计划。例如,如果您想将整数 42 转换为字符串“42”,您可以说:

sendBuffer[0]=count;

Credit for ENOUGHgoes to caf.

信贷ENOUGH咖啡馆

回答by Burkhard

printf(%s,sendBuffer);should be printf("%s",sendBuffer);

printf(%s,sendBuffer);应该 printf("%s",sendBuffer);

回答by Ed Heal

Change

改变

sendBuffer[0]='0' + count;

to

sendBuffer[0]=count + '0';

i.e. convert the integer 0...9 to the characters '0' ... '9'

即将整数 0...9 转换为字符 '0' ... '9'

Also add a quote i.e. printf("%s",sendBuffer);

还添加一个报价即 printf("%s",sendBuffer);

回答by delicateLatticeworkFever

To convert an integer value to a char representation, add the value of the character '0':

要将整数值转换为字符表示,请添加字符“0”的值:

char sendBuffer[1000];
int count=48:
while(count<58)
{
    sendBuffer[0]=count;
    sendBuffer[1]='a';
    sendBuffer[2]='b';
    sendBuffer[3]='c';
    sendBuffer[4]='
char sendBuffer[1000];

// take fixed stuff outside the loop
    sendBuffer[1]='a';
    sendBuffer[2]='b';
    sendBuffer[3]='c';
    sendBuffer[4]='##代码##';

// it's best not to rely on order of ASCII values

for(char* numbers="0123456789"; *numbers!=0; numbers++){// use for loop for numbers
    sendBuffer[0] = *numbers;

    printf("%s",sendBuffer);
}
'; printf(%s,sendBuffer); count=count+1; }

Notice that's the character '0', not the number 0. This is because of how ascii valueswork. The char with a literal value of 0 is \0, the null terminator. Digit '0' has a literal value of 48, '1' 49 and so on.

请注意,'0'这是字符,而不是数字 0。这是因为ascii 值的工作方式。字面值为 0 的字符是\0空终止符。数字“0”的字面值为 48,“1”为 49,依此类推。

This works for the digits 0-9. You can't put a char representation of a number bigger than that in a single char, obviously.

这适用于数字 0-9。显然,您不能在单个字符中放置比数字更大的数字的字符表示。

回答by uba

Quoted from the question:

引自问题:

In the output, all buffer is printed correctly except the first index.i want 1,2,3 and so on to be printed at the start of the buffer but it does not work. Please Help

在输出中,除第一个索引外,所有缓冲区都被正确打印。我希望在缓冲区的开头打印 1,2,3 等,但它不起作用。请帮忙

I think her problem is that she does not get any output for the first line. That is because in the first iteration, count is 0, i.e. sendBuffer[0] is 0 which is the '\0' character. Hence the string is treated as empty.

我认为她的问题是她没有得到第一行的任何输出。那是因为在第一次迭代中,count 为 0,即 sendBuffer[0] 为 0,即 '\0' 字符。因此该字符串被视为空。

回答by Drona

You are trying to print the ascii characters corresponding to decimal values 0-9 which are non-printable characters. If you need to print decimal 0-9 then initialise count to 48 which is the ascii decimal code for '0' and change the condition in the while block to count < 58; 57 is the ascii deciaml code for '9'. See below:

您正在尝试打印与十进制值 0-9 相对应的 ascii 字符,这是不可打印的字符。如果您需要打印十进制 0-9,则将 count 初始化为 48,这是 '0' 的 ascii 十进制代码,并将 while 块中的条件更改为 count < 58;57 是 '9' 的 ascii 十进制代码。见下文:

##代码##

回答by QuentinUK

##代码##