Strings as integers

This is a discussion on Strings as integers within the C Programming forums, part of the General Programming Boards category; Hi, how would I convert a string such as "ThisisASTRing" to an integer. I'm trying to find the key value ...

  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
    535
    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,344
    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%.

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-21-2009, 11:05 PM
  2. Converting Integers to Strings
    By momo97 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:36 AM
  3. Replies: 0
    Last Post: 12-05-2008, 01:16 PM
  4. Replies: 9
    Last Post: 03-17-2006, 11:44 AM
  5. fread - reading strings and integers.
    By Vber in forum C Programming
    Replies: 1
    Last Post: 11-17-2002, 03:08 PM

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