【问题标题】:How to convert integers to characters in C?如何将整数转换为C中的字符?
【发布时间】:2013-11-17 03:09:14
【问题描述】:

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

【问题讨论】:

  • 只是投吗? (字符)神? whr ascii set 和 reverse 中的 theint 也是可能的 (int)thechar
  • @7-isnotbad:我想从 ascii 代码生成一个 ascii 字符。
  • 现在的问题是什么?你看我的评论...

标签: c integer character


【解决方案1】:

在 C 中,int、char、long 等都是整数。

它们通常具有不同的内存大小,因此具有不同的范围,如INT_MIN 到INT_MAX。 char 和 char 的数组通常用于存储字符和字符串。整数以多种类型存储:int 是最流行的,因为它在速度、大小和范围之间取得了平衡。

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

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

要将int 转换为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; 

出现此警告是因为int 通常具有比char 更大的范围,因此可能会发生一些信息丢失。通过使用演员(char),信息的潜在损失被明确地引导。

打印整数的值:

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>

还有其他关于打印的问题,例如在打印unsigned char 时使用%hhu 或强制转换,但留待以后再说。 printf() 有很多内容。

【讨论】:

    【解决方案2】:
    char c1 = (char)97;  //c1 = 'a'
    
    int i = 98;
    char c2 = (char)i;  //c2 = 'b'
    

    【讨论】:

      【解决方案3】:

      将整数转换为 char 即可满足您的要求。

      char theChar=' ';
      int theInt = 97;
      theChar=(char) theInt;
      
      cout<<theChar<<endl;
      

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

      【讨论】:

      • cout&lt;&lt;theChar&lt;&lt;endl; 不是有效的 C 代码。 C++ 的规则与 C 的不同之处在于考虑常量,char 和 int 用于这篇 C 帖子。在 C 中,' ' 是一个int。在 C++ 中,它是 char。
      【解决方案4】:
      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);
      
       }
      

      【讨论】:

      • integer=~integer+1; 是 int 在 integer == INT_MIN 时溢出。这是未定义的行为。此代码生成“count =11 ascii=-./,),(-*,...”ascii[10] 对于许多导致缓冲区溢出的大负值来说太小了。
      【解决方案5】:

      程序将 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 );
      }
      

      程序将字母转换为 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 );
      }
      

      【讨论】:

      • (char)alphabet 中的转换是将对象alphabet(即char)转换为char。当然不需要演员阵容。 (char)num 很奇怪,不需要将char 作为... 参数传递,将其转换为int。 "%c" 指示函数然后将 int 转换为 unsigned char,然后根据该值打印字符。
      猜你喜欢
      • 2021-10-22
      • 2016-01-20
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      相关资源
      最近更新 更多