Thread: Strings as integers

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Strings as integers

    Hi, how would I convert a string such as "ThisisASTRing" to an integer. I'm trying to find the key value for a hash table which has a size around 1055 spaces.

    thanks,
    unknown_

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    You can't get the integer value for a string. But you can get the integer value for a character.

    Example:
    Code:
    #include<stdio.h>
    main()
    {
            printf("%d\n",'a');
    }

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You can use atoi...

    int atoi ( const char * str );

    atoi - C++ Reference


    Or parsing it yourself wouldn't be too hard (though a waste of time IMO). Loop through each character, convert it to numerical value from char encoding (probably ASCII), multiply it by the base and accumulate it.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I suspect what unknown_ is looking for is "string hashing" or "hash function". Searching for those terms using google will provide plenty of information - albeit, like all such things on the internet, a mix of good and bad.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about reading in strings and integers
    By sgalland in forum C Programming
    Replies: 6
    Last Post: 11-22-2009, 12:05 AM
  2. Converting Integers to Strings
    By momo97 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 01:36 AM
  3. Replies: 0
    Last Post: 12-05-2008, 02:16 PM
  4. Replies: 9
    Last Post: 03-17-2006, 12:44 PM
  5. fread - reading strings and integers.
    By Vber in forum C Programming
    Replies: 1
    Last Post: 11-17-2002, 04:08 PM