如何在0-25之间的整数转换为对应的ASCII字符? [英] How to convert a integers between 0 and 25 to corresponding ASCII characters?

查看:205
本文介绍了如何在0-25之间的整数转换为对应的ASCII字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个号 0 对应于ASCII字符 A 。我怎么会去字母表范围 0 25 转换成一个数字来的信吗?

我已经尝试添加 97 来的十进制值,但它只是输出数字+ 97

 的typedef枚举{
    A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y, ž
}设置;无效dispSet(组号码[],诠释size_numbers){
  INT I;
  的printf([);
  对于(I = 0; I和下; size_numbers-1;我++){
    的printf(%d个,((炭)号[I])+ 97);
  }
  的printf(%d个],((炭)号[size_numbers-1])+ 97);
  的printf(\\ n);
}


解决方案

您应该要考取%C 对printf,而不是%d个。格式说明符,告诉printf的如何跨preT提供的paramters。如果你通过%d个,它会间preT参数为整数。通过指定%C ,你告诉它跨preT参数为一个字符。为printf的联机帮助页/帮助下,最终导致一些格式说明,它给你的完整列表。

就个人而言,我倾向于使用 someValue中+'A' someValue中+'A',因为我觉得有点容易按照其中的code。

Say I have a number 0 that corresponds to the ASCII character a. How would I go about converting a number in the range 0 to 25 to letters in the alphabet?

I have already tried adding 97 to the decimal value, but it just outputs the number+97.

typedef enum {
    a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
} set;

void dispSet(set numbers[], int size_numbers) {
  int i;
  printf("[ ");
  for (i = 0; i < size_numbers-1; i++) {
    printf("%d, ", ((char) numbers[i])+97);
  }
  printf("%d ]", ((char) numbers[size_numbers-1])+97);
  printf("\n");
}

解决方案

You should should be pasing %c to printf, not %d. The format specifier, tells printf how to interpret the supplied paramters. If you pass %d, it will interpret the arguments as an integer. By specifying %c, you tell it to interpret the argument as a character. The manpages / help for printf, eventually lead to some 'format specifiers', which gives you the full list.

Personally, I tend to use someValue + 'a', or someValue + 'A', because I find it a bit easier to follow the code.

这篇关于如何在0-25之间的整数转换为对应的ASCII字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆