converting whole strings to integers?
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.
You're limited to the size of an integer which is, on most current platforms, 4 bytes.
This is, however, not exactly portable code.
-Markus-
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-
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,
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,
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,
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.
(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.
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.
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.
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; // or}
See a simple C reference for further information about these operators.
Regards,
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement