Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Strings as integers in C

  1. Mar 19, 2010 #1
    hey, I've hit a bump in the road here. I'm trying to build code for a hash table out of my textbooks pseudo code, and I'm not quite sure how I'd represent a string of characters such as "thisISastring", as an integer for my key variable. i'm using division method if that helps at all. I know the function atoi(), but that's only for number strings right?

    thanks,
     
  2. jcsd
  3. Mar 19, 2010 #2
    First, you probably want to know the size of your hash. If you need in the ballpark of 50 records or less, you can take the ASCII value of the first character in the string, and that's probably ok. If you need more, take the ASCII values of the first two characters, etc.

    If it's a standard "typeable" string, you might want to take the ASCII value and subtract 32, which means each "typical" character would be in the range of 0-94. If that was ok, and you wanted a bigger hash, you could take the ASCII value of the 1st character minus 32, and add it to, say, 95 times the ASCII value of the 2nd character minus 32. And so on.

    If I remember my C correctly, a string is simply an array of characters, and you can simply treat a single character as though it were a 1-byte integer (IE, it's already an integer, probably 0-255).

    DaveE
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Strings as integers in C
  1. Strings in c. (Replies: 3)

Loading...