将字母字符串转换为ASCII码,然后再次返回 [英] Alphabet String to ASCII codes and back again
问题描述
大家好,
我正在尝试将大写字母字符的短字符串转换为相应的ASCII代码(或者是否有更好的方法来获取字符串,因为数字请告诉我).
我想要的是输入大约9个字符的字符串,让它将字符串中的每个字符转换为数值(其中A = 1,B = 2等),以便我可以对其进行一些检查,并附加另一个数字放在末端,然后再次将其吐出为一串字母.
这是出于检查数字的目的,因此在程序的后面,我还想重新计算该检查数字,并将其与新字符串上附加的数字进行比较.
如果没有那么复杂的方法,请提出建议!
感谢
Hi guys,
I''m trying to convert a short string of uppercase alphabetical characters to the corresponding ASCII codes (or if there''s a better way to get the string as numbers let me know please).
What I want is to feed in the string of around 9 chars, have it convert each char in the string to a numerical value (where A=1, B=2 etc) so that I can perform some checks on it, append another number onto the end and spit it back out as a string of letters again.
This is for the purpose of a Check Digit, so later in the program I also want to recalculate the Check Digit and compare it to the appended one on the new string.
If there''s a less convoluted way to do this please suggest!
Thanks
推荐答案
您可以从Unicode代码点值创建校验和,因此:
You can create a checksum from the Unicode code point value thus:
int sum = 0;
foreach(char c in s) sum += (int)c;
如果希望它以A = 1开头,则执行sum += (1 + c - ''A'')
.
If you want it to start with A=1 then do sum += (1 + c - ''A'')
.
这篇关于将字母字符串转换为ASCII码,然后再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!