稍微混淆了一些C#编码 [英] Slightly confused to some C# coding

查看:72
本文介绍了稍微混淆了一些C#编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有这部分代码



I have this part of the code in my program

int remainder = sum % 26;

     byte[] code = Encoding.ASCII.GetBytes(check);
     int chk = code[0];
     chk = chk - 65;                  // Subtract 65 from ASCII value for comparison i.e.
                                      // A = 0, B = 1, C = 2 onwards
     /* If we have check letter let say J its ascii value is 74 so chk will have value 9
        and as security number is 1122334455 sum = 165 and reminder = 9
      */
     if (remainder == chk)
     {





我的讲师帮助我写了那篇文章,但我不明白它的作用。我无法抓住他,我的截止日期是明天。我必须加上10个数字的总和,然后字母表的每个字母都分配了一个数字,所以A = 0,B = 1等等,我没有得到的是J = 74怎么样,为什么我必须-65?如果我从我的程序中取出chk = chk -65代码,那么它就不起作用了。有什么见解?



My lecturer helped me write that part, but I do not understand what it does. I can't get hold of him and my deadline is in for tomorrow. I have to add up the sum of 10 numbers and then each letter of the alphabet is assigned a number so A =0, B=1 etc etc, what i don't get is how does J = 74 and why do I have to -65? If i take the chk = chk -65 code out of my program then it does not work. Any insight?

推荐答案

您使用的映射('A'< - > 0,'B'< - > 1 ,'C'< - > 2,... )用于计算字符串上的校验和,但它通常在计算机上找不到,即ASCII [ ^ ]一个('A'< - > 65,'B'< - > 66,'C'< - > 67,.. 。)。



幸运的是,两个'编码'(你的和 ASCII 一个)只有区别对于偏移,因此



The mapping you use ('A'<->0, 'B'<->1, 'C'<->2, ...) is handy for computing a checksum on a string but it is not the same usually found on computer, that is the ASCII[^] one ('A'<->65, 'B'<->66, 'C'<->67, ...).

Luckily the two 'encodings' (yours and the ASCII one) differ only for an offset, hence

char 你的 = char ASCII - 65
charyours = charASCII - 65


这篇关于稍微混淆了一些C#编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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