[英]Letters to Numbers with C++ chars
Say I have a char extracted from a string ( str.at(i) ), how would I convert that char to a number such that A=0, B=1, C=2... Z=25? 假设我从字符串(str.at(i))中提取了一个char,我该如何将该char转换为A = 0,B = 1,C = 2 ... Z = 25的数字? Thanx in advance 提前感谢
Assuming that the string is already in the AZ
range, you could do char_value - 'A'
. 假设字符串已经在AZ
范围内,则可以执行char_value - 'A'
。
This assumes that the letters are all consecutive. 这假定字母都是连续的。 So 'B' == 'A' + 1
, 'C' == 'A' + 2
, etc. In ASCII, this assumption is correct. 所以'B' == 'A' + 1
, 'C' == 'A' + 2
,依此类推。在ASCII中,此假设是正确的。
Each Character has a specific ASCII code !! 每个字符都有一个特定的ASCII码! Like A = 65 , b = 66 .. etc !! 像A = 65,b = 66 ..等等! If you simply subtract 65 or 'A' from each character , you will get the desired int 如果您简单地从每个字符中减去65或'A',您将获得所需的int
eg : 例如:
int a = charArray[i] - 65;
if: charArray[i] = A
then: a = 0
& so on!! 等等!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.