Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
     {


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?
Posted
Comments
BillWoodruff 19-Jan-14 16:20pm    
C. Pallini gives you an excellent explanation below; I'd add that there's no "mystery" here; the values of the ASCII character codes are just arbitrary left-overs ... fossils ... of the evolution of computer programming; they originated in telegraphic codes.

Upper-case ASCII characters are between #65 and #90, lower-case are from #97 to #122.

Sometimes it's a mistake to assume that there's any deep "principle" at work :)

1 solution

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

charyours = charASCII - 65
 
Share this answer
 
Comments
Member 10506451 19-Jan-14 16:38pm    
thanks for the help :)
Member 10506451 19-Jan-14 16:49pm    
I have another question about what some code does if that is ok? should i just post it here or create a new question thread?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900