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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20051619/
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 10:20:21  来源:igfitidea点击:

Store an integer in a char array

carrays

提问by user2426316

I am trying to store an integer in a chararray. How can I do that? This is my approach (by casting it the intto a char) but it does not work. What am I missing?

我想在char数组中存储一个整数。我怎样才能做到这一点?这是我的方法(通过将其转换int为 a char)但它不起作用。我错过了什么?

#include <stdio.h>

int main(int argc, char** argv) 
{
    char cArray[10] = {};

    // Store a character in the char array
    cArray[5] = 'c';
    printf("%c\n", cArray[5]);

    // Store an integer in the char array
    cArray[6] = (char) 0; // WHY DOES THIS NOT WORK???
    printf("%c\n", cArray[6]);
}

采纳答案by John Bode

cArray[6] = (char) 0; // WHY DOES THIS NOT WORK???
printf("%c\n", cArray[6]);

This code attempts to print a characterwith the encoding 0; assuming ASCII, nothing will get displayed because there is no printable character associated with that code.

此代码尝试打印具有编码的字符0;假设是 ASCII,则不会显示任何内容,因为没有与该代码关联的可打印字符。

If you intend to store the ASCII code for the character'0'and print it, then you need to write

如果要存储的ASCII码的字符'0',并打印出来,那么你需要写

cArray[6] = 48;               // same as writing cArray[6] = '0' (ASCII)
printf( "%c\n", cArray[6] );

This will print 0to your console.

这将打印0到您的控制台。

If, on the other hand, you want to store anyarbitrary integer value1to cArray[6]and display that value, then you need to use the %dconversion specifier:

如果,另一方面,你要存储任何任意整数值1cArray[6]并显示该值,则需要使用%d转换符:

cArray[6];
printf( "%d\n", cArray[6] );



1. That is, any integer that fits into the range of char, anyway1. 也就是说,任何符合 范围的整数char,无论如何

回答by Se?kin Sava???

Let's start with the basics.

让我们从基础开始。

For x86 architecture(assuming you use it) a charvariable is stored in 1 byte and an intvariable is stored in 4 bytes.

对于 x86 架构(假设您使用它),一个char变量存储在 1 个字节中,一个int变量存储在 4 个字节中。

It is IMPOSSIBLE to store a randominteger value in a char variable unless you have some compression schema and know that some integer values will not occur(no random!) in your program. No exceptions.

random除非您有一些压缩模式并且知道程序中不会出现某些整数值(不是随机的!),否则将整数值存储在 char 变量中是不可能的。没有例外。

In your case, you want to store integers in a char array. There are four options:

在您的情况下,您希望将整数存储在 char 数组中。有四个选项:

1.If you want to store a random integer in this char array, then you should get the pointer to the index that you want to store the integer and cast it to an integer pointer and use it like that.

1.如果你想在这个字符数组中存储一个随机整数,那么你应该获取指向你想要存储整数的索引的指针,并将其转换为整数指针并像这样使用它。

char mychars[10];
int * intlocation = (int*)(&mychar[5]);
*intlocation = 3632; // stores 3632

Beware that this will write to 4 bytes(4 char locations in your array) starting from the index you have specified. You should always check you are not going out of array.Also you should do the same casting for retrieving the value when needed.

请注意,这将从您指定的索引开始写入 4 个字节(数组中的 4 个字符位置)。您应该始终检查您没有超出数组。此外,您应该在需要时进行相同的转换以检索值。

2.If your values are between [0,255] or [-128,127] you can safely store your integers in a char since these ranges can be represented using a byte. Beware that char being signed or unsigned is implementation-dependent. Check this out!

2.如果您的值介于 [0,255] 或 [-128,127] 之间,您可以安全地将整数存储在字符中,因为这些范围可以使用字节表示。请注意,有符号或无符号的 char 取决于实现。看一下这个!

mychars[5] = 54;

3.If your integer is just a digit, you can use the char representation of the digit.

3.如果你的整数只是一个数字,你可以使用数字的char表示。

mychars[5] = your_digit + 48; // 48 is the ascii code for '0'

4.If you want to store the string representation of your integer, then you should use itoa()and write each char of the resulting string to your array one by one. In that case, you should always check that you are not going out of array.

4.如果要存储整数的字符串表示形式,则应使用itoa()结果字符串的每个字符并将其一一写入数组。在这种情况下,您应该始终检查您没有超出阵列。

回答by glglgl

Normally, a computer exactly does what you tell it. The instruction DWIM(do what I mean) hasn't been invented yet.

通常,计算机完全按照您的指示执行操作。指令DWIM(做我的意思)还没有被发明出来。

cArray[6] = (char) 0; // WHY DOES THIS NOT WORK???

sets the index position 6, but

将索引位置设置为 6,但是

printf("%c\n", cArray[5]);

prints the index position 5.

打印索引位置 5。

回答by benjarobin

Replace

代替

cArray[6] = (char) 0; // WHY DOES THIS NOT WORK???
printf("%c\n", cArray[5]);

By

经过

cArray[6] = (char)51;
printf("%c\n", cArray[6]);

Should display '3'

应该显示“3”

I think you will understand your mistake... 0 as int does not represent the printable character '0', but the NULL termination string

我想你会明白你的错误... 0 as int 不代表可打印字符 '0',而是 NULL 终止字符串

51 as int represent the character '3'

51 as int 代表字符'3'

回答by deeiip

You may use itoa(0,&cArry[6],10).

您可以使用itoa(0,&cArry[6],10).

回答by chux - Reinstate Monica

Use the correctly desired format specifier.

使用正确所需的格式说明符。

cArray[6] = (char) 0;doesstore an integer 0 into array element cArray[6]. Its the printf()that is fooling OP into thinking that did not work.

cArray[6] = (char) 0;确实将整数 0 存储到数组 element 中cArray[6]。它printf()是愚弄OP认为不起作用的。

Using %csays OP wants the character that is encoded with a 0. (It usually not visible). Use %d to print the integer value of cArray[6].

Using%c说 OP 想要用 0 编码的字符。(它通常不可见)。使用 %d 打印 的整数值cArray[6]

printf("%d\n", cArray[6]);

回答by user1462840

You can add the ASCII value of 0 to the value you want to store digit into the character array. For example if you want to store 0,1,...,9 into the character array A[10], you may use the following code.

您可以将 0 的 ASCII 值添加到要存储数字到字符数组中的值。例如,如果要将 0,1,...,9 存储到字符数组 A[10] 中,则可以使用以下代码。

     for(j = 0; j<k; j++)
     {
          A[j] = j + '0';  // Same effect if you use A[j] = j + 0x30
     }

This works only if the integer you want to store is less than 10. Otherwise you will have to extract the digits using modulo operator and store.

这仅在您要存储的整数小于 10 时才有效。否则您将不得不使用模运算符提取数字并存储。