Sign in to follow this  
GameMasterXL

converting whole strings to integers?

Recommended Posts

GameMasterXL    100
Is there a way to ocnverte whole strings like char name[5]="John"; into an integer value? I tried some of conversion but it only converted the first character and left the rest.

Share this post


Link to post
Share on other sites
Cygon    1219
You're limited to the size of an integer which is, on most current platforms, 4 bytes.


int main() {
// With run-time conversion
char name[] = "John";
int name_as_int = *reinterpret_cast<int *>(name);

// Or, alternatively
name_as_int = 'John';
}



This is, however, not exactly portable code.

-Markus-

Share this post


Link to post
Share on other sites
What should be the integer value of "John"? Are you trying to compute some kind of checksum that will identify a particluar string? If yes, I suggest you to search the web for "string hash function" (for example, this page has rather interesting stuff).

To converta string to an integer, you have no choice but looping on the string until its end - ar any other stop condition you want to use.

HTH,

Share this post


Link to post
Share on other sites
Quote:
Original post by Cygon
You're limited to the size of an integer which is, on most current platforms, 4 bytes.

*** Source Snippet Removed ***

This is, however, not exactly portable code.

-Markus-


What the hell is

name_as_int = 'John';

?!?!?

This is of course non portable code. It is actually non compilable code. The first part of your main is gramatically correct (the reinterpret_cast<> thing), but is limited to strings with at least 4 chars (a string with 1 char (the NUL char) will probably choke or at least won't give you the result you want) and do not take in account the remaining chars.

Regards,

Share this post


Link to post
Share on other sites
GameMasterXL    100
So does the casting method work?i havn't been able to try it yet. I am not well up on hash tables yet. Is there no easy ish way to change the name into characters? i am writing a parser and found that searching an array for a specific variable name was hard so i thought about converting the variable name into an integral value and then storing that into a variable name array and searching that instead.

Share this post


Link to post
Share on other sites
(edit: extended)

Hash function have little to do with hash tables. Hash tables uses hash function, but hash functions can exists without hash tables.

The casting method simply change the type of the 4 bytes that are at the beginning of the string. Imagine you have the following memory layout ('.' are non-ascii values)

. . . . a b c d e \0

Your string points on a. If you transform the pointer into a pointer to an int (this is what the cast operator do), then your pointer will still point to the same location. Now, dereferencing the pointer will read the 4 bytes (abcd), and not only the 1st one.

[angry] A bump after only 21 minutes of wait is not a polite bump. Are you dying? I bet you are not. We may have something other to do. Obviously your thread is still in the first 20 threads of the forum, and it don't need a bump.

Politeness is something thet is commonly appreciated by the board members.

Bye.

Share this post


Link to post
Share on other sites
GameMasterXL    100
One problem though is if i do character by character then how would i combine all the conversioned characters together into one integer? i don't even know how to turn one character into a number i don't think. So could you pleas explaib a way of doing this pleas. Sory about that.

Share this post


Link to post
Share on other sites
Quote:
Original post by GameMasterXL
One problem though is if i do character by character then how would i combine all the conversioned characters together into one integer? i don't even know how to turn one character into a number i don't think. So could you pleas explaib a way of doing this pleas. Sory about that.


You can use the bitshift operator in conjunction with the bitwise operators:


char buffer[5] = "John";
int john = 0;
for (int i=0; i<4; i++)
{
john = john << 8; // shift by 8 bits.
john = john | buffer[i]; // or
}


See a simple C reference for further information about these operators.

Regards,

Share this post


Link to post
Share on other sites
DrEvil    1148
I character is an 8 bit number. I would suggest looking up a hash function. Usually shifting involves bit manipulation. Good hash methods are the ones with the least chance of collisions. I believe this is fairly difficult to just make one up when dealing with strings.

If you want a simple example of a hash function(non string though), consider this.

I've seen hash functions used in A* pathfinding before. Suppose you have a 2d grid of tiles, and in your closed list you want to be able to look up a tile very quickly using a unique key. A hash function for this type of situation could be as simple as.

int simple_hash(int tilex, int tiley)
{
int iHash = (tilex << 16) | tiley;
return iHash;
}

This operates on the assumption that you will only ever really need 16 bits for each index of your tile map. By shifting the x index of the tile over, and OR'ing with the y index you can produce a nice unique key for this particular tile that could be used in a hash table, an std::map, or whatever.

If the dimensions of your tile map are > 65,535 you probably have bigger problems.


Share this post


Link to post
Share on other sites
DrEvil    1148
Simply shifting and ORing isn't a good hash function for strings. Every string hash function I've seen involves other bit operators as well. xor/not/... operators . Strictly using | (OR) only turns bits on. The collision rate is probably crazy high with this method because it may only take a few characters worth of shifting and ORing to turn on all or most of the bits in your hash value.

It's not easy for string values. I suggest looking up some code for CRC. There's code all over the net that calculate CRC based on arbitrary byte buffers and strings. You will notice right off the bat that in order to do it well, it can get complicated.

If you're not familiar with bit operations | ^ & then I'm afraid you won't understand much from hash functions most likely.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this