Like Tree1Likes
  • 1 Post By oogabooga

Convert string to ASCII values

This is a discussion on Convert string to ASCII values within the C Programming forums, part of the General Programming Boards category; So I have a hashing function whereby I have to take a string as my key, get the sum of ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    Convert string to ASCII values

    So I have a hashing function whereby I have to take a string as my key, get the sum of it's ASCII values and modulo by a given size.

    I realize this is relatively simple and can be accomplished through the use of sprintf but I cannot find any clear examples on how to do so.

    If anyone can give me an example it would be much appreciated as this is all I really need help on, and I can figure out the rest from there.


    Edit: to be clear, I only need help on figuring out the string to ascii conversion
    Last edited by jeanermand; 03-05-2012 at 05:13 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't see how sprintf will help. Just loop through the string and sum the value of its individual characters.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    So something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void)
    {
        char string[50];
        int i, sum;
        scanf("%s", string);
        sum = 0;
        for(i = 0; i < strlen(string); i++)
            sum += (int)string[i];
    
        printf("(%d)\n", sum);
        system("pause");
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Right. There's no sprintf in there, hence oogabooga's confusion and question. You're 99% of the way there. You already add up the ASCII values. The only thing you're missing is the modulo: sum % some_size.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    My bad, the sprintf is for the second hashing function where we have to write the ASCII values to a string.

    To complete this though:
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    
    #define SIZE 10  
    int main (void) 
    {     
        char string[50];     
        int i, sum, address;     
        scanf("%s", string);     
        sum = 0;     
        for(i = 0; i < strlen(string); i++)         
            sum += (int)string[i];
    
        address = sum % SIZE;     
        printf("(%d)(%d)\n", sum, address);     
        system("pause");     
        return 0; 
    }
    The size is predetermined for our assignment. In this case, we are using 10 to test our collision resolution method

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That looks good. A couple of points:
    1. You could use a format of "%49s" in your scanf to protect from possible buffer overflow. And note that %s (with or without the size limit) will not read in a string with a space in it, but that may be the behavior you want.
    2. It might be better to cast to unsigned char instead of int for the summation since, if chars are signed (and they may or may not be!), summing them (casted to int or not) will actually subtract ... however that may be the behavior you want. I suppose it doesn't matter as long as you're consistent.
    laserlight likes this.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by oogabooga
    2. It might be better to cast to unsigned char instead of int for the summation since, if chars are signed (and they may or may not be!), summing them (casted to int or not) will actually subtract ... however that may be the behavior you want. I suppose it doesn't matter as long as you're consistent.
    That said, the characters from the original ASCII set will be non-negative as signed char.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-04-2011, 05:42 PM
  2. Convert ASCII to a string
    By Toonzaka22 in forum C Programming
    Replies: 2
    Last Post: 11-29-2009, 08:59 PM
  3. ASCII convert to Hexadecimal value?
    By ashish.malviya in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:45 AM
  4. How to Convert Space in ascii?
    By larkin10 in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 05:57 AM
  5. convert string to ASCII value
    By ling in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2002, 08:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21