Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding ASCII values in Char Array, C#

    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.

    Code:
            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.
    Last edited by digink; 01-29-2011 at 05:10 PM. Reason: Resolved.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You want to take the third character in the string and multiply it by 3; then return it?

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    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.

  • #4
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by digink View Post
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •