C语言 如何将整数转换为C中的字符?

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

How to convert integers to characters in C?

cintegercharacter

提问by Derpster13

For example, if the integer was 97, the character would be 'a', or 98 to 'b'.

例如,如果整数是 97,则字符将是 'a',或 98 到 'b'。

回答by chux - Reinstate Monica

In C, int, char, long, etc. are all integers.

在 C 中intcharlong、 等都是整数

They typically have different memory sizes and thus different ranges as in INT_MINto INT_MAX. charand arrays of charare often used to store characters and strings. Integers are stored in many types: intbeing the most popular for a balance of speed, size and range.

它们通常具有不同的内存大小,因此在INT_MINto 中的范围也不同INT_MAXchar和数组char通常用于存储字符和字符串。整数以多种类型存储:int在速度、大小和范围的平衡方面最流行。

ASCII is by far the most popular character encoding, but others exist. The ASCII code for an 'A' is 65, 'a' is 97, '\n' is 10, etc. ASCII data is most often stored in a charvariable. If the C environment is using ASCII encoding, the following all store the same value into the integer variable.

ASCII 是迄今为止最流行的字符编码,但也存在其他字符编码。'A' 的 ASCII 码是 65,'a' 是 97,'\n' 是 10,等等。ASCII 数据最常存储在一个char变量中。如果 C 环境使用 ASCII 编码,以下都将相同的值存储到整数变量中。

int i1 = 'a';
int i2 = 97;
char c1 = 'a';
char c2 = 97;

To convert an intto a char, simple assign:

要将 an 转换int为 a char,简单的赋值:

int i3 = 'b';
int i4 = i3;
char c3;
char c4;
c3 = i3;
// To avoid a potential compiler warning, use a cast `char`.
c4 = (char) i4; 

This warning comes up because inttypically has a greater range than charand so some loss-of-information may occur. By using the cast (char), the potential loss of info is explicitly directed.

出现此警告是因为int通常具有更大的范围char,因此可能会发生一些信息丢失。通过使用 cast (char),可以明确指示潜在的信息丢失。

To print the value of an integer:

打印一个整数的值:

printf("<%c>\n", c3); // prints <b>

// Printing a `char` as an integer is less common but do-able
printf("<%d>\n", c3); // prints <98>

// Printing an `int` as a character is less common but do-able.
// The value is converted to an `unsigned char` and then printed.
printf("<%c>\n", i3); // prints <b>

printf("<%d>\n", i3); // prints <98>

There are additional issues about printing such as using %hhuor casting when printing an unsigned char, but leave that for later. There is a lot to printf().

关于打印还有其他问题,例如%hhu在打印 时使用或强制转换unsigned char,但留待以后处理。有很多事情要做printf()

回答by nhgrif

char c1 = (char)97;  //c1 = 'a'

int i = 98;
char c2 = (char)i;  //c2 = 'b'

回答by DanChianucci

Casting the integer to a char will do what you want.

将整数转换为字符会做你想要的。

char theChar=' ';
int theInt = 97;
theChar=(char) theInt;

cout<<theChar<<endl;

There is no difference between 'a' and 97 besides the way you interperet them.

除了您解释它们的方式之外,'a' 和 97 之间没有区别。

回答by kannadasan

void main ()
 {
    int temp,integer,count=0,i,cnd=0;
    char ascii[10]={0};
    printf("enter a number");
    scanf("%d",&integer);
     if(integer>>31)
     {
     /*CONVERTING 2's complement value to normal value*/    
     integer=~integer+1;    
     for(temp=integer;temp!=0;temp/=10,count++);    
     ascii[0]=0x2D;
     count++;
     cnd=1;
     }
     else
     for(temp=integer;temp!=0;temp/=10,count++);    
     for(i=count-1,temp=integer;i>=cnd;i--)
     {

        ascii[i]=(temp%10)+0x30;
        temp/=10;
     }
    printf("\n count =%d ascii=%s ",count,ascii);

 }

回答by Amol Damodar

Program Converts ASCII to Alphabet

程序将 ASCII 转换为字母

#include<stdio.h>

void main ()
{

  int num;
  printf ("=====This Program Converts ASCII to Alphabet!=====\n");
  printf ("Enter ASCII: ");
  scanf ("%d", &num);
  printf("%d is ASCII value of '%c'", num, (char)num );
}

Program Converts Alphabet to ASCII code

程序将字母转换为 ASCII 码

#include<stdio.h>

void main ()
{

  char alphabet;
  printf ("=====This Program Converts Alphabet to ASCII code!=====\n");
  printf ("Enter Alphabet: ");
  scanf ("%c", &alphabet);
  printf("ASCII value of '%c' is %d", alphabet, (char)alphabet );
}