...

View Full Version : Resolved Adding ASCII values in Char Array, C#



digink
01-29-2011, 05:27 AM
I was hoping someone could help me with this C# code, for some reason I'm having a hard time wrapping my head around getting my code to SUM these individual ASCII values... I know their has to be an easier way than adding them to an int array and then summing the array.



private string CheckSum(string input)
{
try
{
string returnV = string.Empty;

char[] charArray = input.ToCharArray();

int i = 0;

foreach (char c in charArray)
{
i = i + 1;
if(i==3)
{
returnV += (Convert.ToInt32(c)*3);
}
else
{
returnV += Convert.ToInt32(c);
}
}


return returnV;
}
catch(System.Exception excep)
{
input = excep.Message;
return input;
}
}


This returns the values I'm expecting, except I just want to add them as numbers at the end.

Inigoesdr
01-29-2011, 03:50 PM
You want to take the third character in the string and multiply it by 3; then return it?

digink
01-29-2011, 05:44 PM
You want to take the third character in the string and multiply it by 3; then return it?

No what that code is doing now is what I need it to do, I just need it to actually add the values.

So right now if I pass it this CheckSum("1111") it will return: 494914749 (the ascii values with the 3rd one multiplied by 3).

That is totally correct, but I just need it to actually add them and return the sum.. so I want it to return 294.

digink
01-29-2011, 06:09 PM
No what that code is doing now is what I need it to do, I just need it to actually add the values.

So right now if I pass it this CheckSum("1111") it will return: 494914749 (the ascii values with the 3rd one multiplied by 3).

That is totally correct, but I just need it to actually add them and return the sum.. so I want it to return 294.

doh... I just figured it out after typing that. Instead of passing it to an string variable (returnV) I passed it to an int variable and it works how I need it to.

Thanks for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum