# turning string into int

This topic is 4441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I feel like an idiot, because I saw the answer here a while ago but now I can't seem to find it... I'm storing data by identifier (using std::map). My problem is that the identifier comes in the form of a string, which (the way I understand it) is a slow way of doing it, because string comparisons take a while... So what I would like to do is to change the string into a numerical value. A 64bit variable should be able to hold the equivalent of 8 chars, which should be sufficient. Now, how would I go about casting that?

##### Share on other sites
From a string to an int: atoi( (yourstring)_cstr() )
To a float: atof( (yourstring)_cstr() )
There's also a Boost method but I've never used it before.

The other methods are in this thread.

##### Share on other sites
Quote:
 Original post by Sol462From a string to an int: atoi( (yourstring)_cstr() )To a float: atof( (yourstring)_cstr() )There's also a Boost method but I've never used it before.The other methods are in this thread.
I don't think that's the kind of 'casting' he means. I think he wants to map arbitrary strings to integer values which will then serve as keys in a std::map.

##### Share on other sites
What your looking for is called a hash function (for a more general "data" to number formula). You won't have an 8 char limit with a hash function and you could use 32 bit numbers which are faster on most systems.

Some info and links on Wikipedia.

##### Share on other sites
Quote:
 Original post by methinksI feel like an idiot, because I saw the answer here a while ago but now I can't seem to find it...I'm storing data by identifier (using std::map). My problem is that the identifier comes in the form of a string, which (the way I understand it) is a slow way of doing it, because string comparisons take a while...So what I would like to do is to change the string into a numerical value. A 64bit variable should be able to hold the equivalent of 8 chars, which should be sufficient. Now, how would I go about casting that?

One way to do it would be with bitwise operations: Grab each the numeric value of each character in the string, copy it into the lowest byte of the int64, then shift the int64 8 bits and repeat.

Another (easier but uglier) way would be to get a char* to the int64 (using reinterpret_cast) and copy the characters in the string into it, i.e.

const char* mystring = "abcdefgh";__int64 myint;char* pint = reinterpret_cast<char*>(&myint);for ( int i = 0; i < 8; ++i )    pint = mystring;

Quote:
 Original post by CocalusWhat your looking for is called a hash function.Some info and links on Wikipedia.

Yeah, a hash function would be a good idea if you want to drop the 8-character limit on keys. :)

Edit: Actually, maybe not, since he's using a map. Collisions between hashes would cause problems. Might as well just use a proper hash table.

##### Share on other sites
Quote:
Original post by lancekt
Quote:
 Original post by methinksI feel like an idiot, because I saw the answer here a while ago but now I can't seem to find it...I'm storing data by identifier (using std::map). My problem is that the identifier comes in the form of a string, which (the way I understand it) is a slow way of doing it, because string comparisons take a while...So what I would like to do is to change the string into a numerical value. A 64bit variable should be able to hold the equivalent of 8 chars, which should be sufficient. Now, how would I go about casting that?

One way to do it would be with bitwise operations: Grab each the numeric value of each character in the string, copy it into the lowest byte of the int64, then shift the int64 8 bits and repeat.

Another (easier but uglier) way would be to get a char* to the int64 (using reinterpret_cast) and copy the characters in the string into it, i.e.

*** Source Snippet Removed ***

How can you possibly understand std::map, and know enough to use reinterpret_cast, but not be using std::string? :( Also, standard library algorithms roxor:

__int64 firstEightCharactersAsValue(const std::string& s) {  __int64 result;  const char* c = s.c_str();  std::copy(c, c + 8, reinterpret_cast<char*>(&result));  return result;}

##### Share on other sites
Quote:
 Original post by ZahlmanHow can you possibly understand std::map, and know enough to use reinterpret_cast, but not be using std::string? :( Also, standard library algorithms roxor:__int64 firstEightCharactersAsValue(const std::string& s) { __int64 result; const char* c = s.c_str(); std::copy(c, c + 8, reinterpret_cast(&result)); return result;}

Better to use trivial language features to demonstrate trivial concepts, if you ask me. I don't see what the library stuff really adds here, except complexity.

##### Share on other sites
What, because manually iterating over things, dealing with memory management on strings (granted there isn't any in the sample code, but presumably the string we're interested in "comes from somewhere" at runtime), etc. is "less complex"? Is this some kind of joke?

##### Share on other sites
Quote:
 Original post by ZahlmanWhat, because manually iterating over things, dealing with memory management on strings (granted there isn't any in the sample code, but presumably the string we're interested in "comes from somewhere" at runtime), etc. is "less complex"? Is this some kind of joke?

Any programmer alive can understand iteration over an array. There may be some who are (god forbid) unfamiliar with <algorithm> or even <string>.

Additionally, you are getting a pointer to the raw character data from the std::string in your version. The only real difference is that you have added an extra layer of complexity--the string class, which has no relevance whatever to this discussion.

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by lancekt
Quote:
 Original post by methinksI feel like an idiot, because I saw the answer here a while ago but now I can't seem to find it...I'm storing data by identifier (using std::map). My problem is that the identifier comes in the form of a string, which (the way I understand it) is a slow way of doing it, because string comparisons take a while...So what I would like to do is to change the string into a numerical value. A 64bit variable should be able to hold the equivalent of 8 chars, which should be sufficient. Now, how would I go about casting that?

One way to do it would be with bitwise operations: Grab each the numeric value of each character in the string, copy it into the lowest byte of the int64, then shift the int64 8 bits and repeat.

Another (easier but uglier) way would be to get a char* to the int64 (using reinterpret_cast) and copy the characters in the string into it, i.e.

*** Source Snippet Removed ***

How can you possibly understand std::map, and know enough to use reinterpret_cast, but not be using std::string? :( Also, standard library algorithms roxor:

__int64 firstEightCharactersAsValue(const std::string& s) {  __int64 result;  const char* c = s.c_str();  std::copy(c, c + 8, reinterpret_cast<char*>(&result));  return result;}

Why get the c_str()? Couldn't just do:

__int64 firstEightCharactersAsValue(const std::string& s)
{
__int64 result;
std::copy(s.begin(),s.begin()+sizeof(__int64), reinterpret_cast<char*>(&result));
return result;
}

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
632567
• Total Posts
3007113

×