Jump to content
  • Advertisement
Sign in to follow this  
methinks

turning string into int

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by Sol462
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.
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by methinks
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?


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 Cocalus
What 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 this post


Link to post
Share on other sites
Quote:
Original post by lancekt
Quote:
Original post by methinks
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman

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;
}


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by lancekt
Quote:
Original post by methinks
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?


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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!