-
Convert hex back to int?
Hi. I have a file to read, in this format:
Code:
0,0,0,0,0,0,0,0,0,0,6,0x01
1,0,0,0,0,0,0,0,0,0,0,0x02
2,0,0,0,0,0,0,0,0,0,0,0x04
My function is this:
Code:
void Tile::ReadTileData(char* line)
{
char* buffer;
buffer = strtok (line, ",");
while (buffer != NULL)
{
tile_data.push_back(atoi(buffer));
buffer = strtok (NULL, ",");
}
}
Now this reads all values fine and stores them as ints, but the last hex value is always stored as 0. Is there any way to get it to parse the string in as an int?
-
You need to recognize the prefix 0x. Then you parse the remainder as a hex string. If you used C++, you can use stringstream's setbase to do that.
-
Or use "strtol" which automatically recognizes decimal, hex and octal numbers unless you specify a base.
--
Mats