用C字母转换为数字 [英] Converting Letters to Numbers in C

查看:197
本文介绍了用C字母转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个code,将转换成字母数字。例如
A ==> 0
乙==> 1
ç==> 2
等等。 if语句写26即时通讯思想。我不知道是否有更好的方法来做到这一点...

I'm trying to write a code that would convert letters into numbers. For example A ==> 0 B ==> 1 C ==> 2 and so on. Im thinking of writing 26 if statements. I'm wondering if there's a better way to do this...

感谢您!

推荐答案

如果您需要处理大写和小写,那么你可能需要做这样的事情:

If you need to deal with upper-case and lower-case then you may want to do something like:

if (letter >= 'A' && letter <= 'Z')
  num = letter - 'A';
else if (letter >= 'a' && letter <= 'z')
  num = letter - 'a';

如果你想显示这些,那么你将要添加一个0将其添加到数字转换成ASCII值:

If you want to display these, then you will want to convert the number into an ascii value by adding a '0' to it:

  asciinumber = num + '0';

这篇关于用C字母转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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