Strings as integers in C

  • Thread starter unknown_2
  • Start date
  • #1
29
0

Main Question or Discussion Point

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,
 

Answers and Replies

  • #2
664
3
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
 

Related Threads on Strings as integers in C

Replies
6
Views
5K
  • Last Post
Replies
3
Views
2K
Replies
1
Views
11K
  • Last Post
Replies
16
Views
7K
  • Last Post
Replies
12
Views
9K
Replies
3
Views
13K
Replies
4
Views
45K
Replies
7
Views
3K
Top