Strings as integers in C

  • Thread starter unknown_2
  • Start date
  • #1
unknown_2
29
0
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
davee123
672
4
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
 

Suggested for: Strings as integers in C

Replies
5
Views
353
  • Last Post
Replies
34
Views
309
Replies
17
Views
683
  • Last Post
Replies
9
Views
183
  • Last Post
4
Replies
118
Views
4K
  • Last Post
Replies
5
Views
3K
Replies
4
Views
593
  • Last Post
Replies
11
Views
863
Replies
35
Views
1K
Replies
1
Views
309
Top