简体   繁体   English

将int值添加到C中的char数组

[英]Adding int value to a char array in C

I would like to add an int value in a char array. 我想在char数组中添加一个int值。 meaning if I have a 1 , I would like to represent it like '1' . 意思是如果我有一个1 ,我想用'1'来表示它。

I have seen an equation to do this before to get the ASCII code of it. 在获得它的ASCII码之前,我已经看到了执行此操作的公式。 am working with a limited compiler for C so I don't have the luxury to use functions like sprintf() or others. 我正在使用有限的C编译器进行工作,因此我无法使用sprintf()或其他函数。 it has to be in the form of an equation that I must implement. 它必须采用我必须实现的方程式的形式。 Can anybody help me with that. 有人可以帮我吗?

example of what I'd like to do 我想做的事的例子

char array[2];
char array[0] = 1 * (equation);

and then array[0] should have the value '1'. 然后array[0]的值应为'1'。

If you're working with values in range 0-9, you can use 如果您要使用0-9范围内的值,则可以使用

array[0] = 1 + '0';

This will give you the char representation ( '1' )of int values ( 1 ). 这将为您提供int值( 1 )的char表示( '1' )。

One way is to add ASCII value of 0 to the already present number. 一种方法是将ASCII值0添加到已经存在的数字中。

Suppose int t=8; 假设int t = 8; then we can convert t to char as follows 然后我们可以将t转换为char,如下所示

int t=8;

char s= t + 48; //ASCII value of '0'

So your equation should be 所以你的方程应该是

char s= integer + 48 ; char s =整数+ 48;

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

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