Advertisement

need help converting char's read in from a file to ints

Started by September 13, 2000 01:58 AM
8 comments, last by Quantum 24 years, 4 months ago
im trying to get my level loading in my side scroller sorted out.. and atm it works fine apart from one thing: it loads in the values from the text file, my map looks like this 1111122211221 1221221122112 ...etc it loads them in and copies them to my data...but it copies them as chars and i need them as ints so: memcpy(GameLevels[LevelIndex].Data[row],&String[start], 20); works, but it doesn''t store the data correctly and i tried putting (int) in front of &String.. i tried using atoi() on it, and it gave me an invalid parameter error both times... and i know it loads up fine, because instead of the above, this works:
    
for(int x=0;x<WORLD_SIZEX;x++)
	{
		switch(String[start+x])
			{
			case ''1'':
				GameLevels[LevelIndex].Data[x][row]	= 1;			break;
			case ''2'':
				GameLevels[LevelIndex].Data[x][row]	= 2;			break;							}
	}
    
and it doesn''t work if i remove the '' from 1 & 2. ..so in short, i need an alternative method to convert atoi because i dont wanna run through that loop listed there for each row, for each level in my game
I don''t think you have many choices, you can either cast them to int''s when you read them in, or when you use them within the code, personally you are better doing it when reading your level data in, the loop code in your post should not have much of an overhead..............

Cheers --- PYTHON
Advertisement
While your loading in your map try taking 48 off the value of the char (48 is the ACSII value of 0) this will automatically convert your map to int's, even if you are reading them into char's.

so your code below now comes to :

        for( int x = 0; x < WORLD_SIZEX; x ++ )  GameLevels[LevelIndex].Data[x][row] = String[start + x] - 48;        


there you go, also using the above method you can use the characters from 32(A space) upto 126(~) for your tiles and have a bit more room to play with instead of only having 10 tiles you can now have about 94 different tiles. If you do decide to change the start of your tiles to ! then change the 48 above to 33.

Grab yourself a copy of the ASCII table this will allow you to see what chars are available between 32 and 128 (the printable range of the ASCII table)

I'd forget my head if it was not screwed on! I forgot a bit on the code...

Edited by - Steven on September 13, 2000 3:49:07 PM
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
thanks a lot Steven!! it works great!
How would one print the special ASCII symbols like the face, heart, etc... ?
Peon
you could try
cout << (char)num;
where num is the value of the ascii char you wanna print
look up ascii values in the MSDN
Advertisement
Under windows you would have to use the terminal font and out put the chars 1 or 2 for a smiley, etc but I''m not sure how this would work under windows as under DOS those chars have special meanings ( char 7, a big floating dot, is the bell char so printf("%c",7); would produce a bell sound).
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
I wouldn''t trust the ''-48'' routine, because if you ever want to go over 10 chars, you''ll be hosed. Use the atoi() standard library function to convert from a char (''a'') to an int (''i'')...

Vyvyan
quote: Original post by VyvyanBasterd

I wouldn''t trust the ''-48'' routine, because if you ever want to go over 10 chars, you''ll be hosed. Use the atoi() standard library function to convert from a char (''a'') to an int (''i'')...

Vyvyan


How''s that? I''ve used that code before, and had about 36 + different tiles.

After 0123456789 you have : :;<=>?@ABCDEFG....

so if you use the ''-48'' routine you have access to more than 10 options. Plus the file format that is getting used is one char per tile, I belive, so the atoi() fnction would bomb out if you wanted to use tile #11 (character : ), unless Quantum changed his file format.

When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
Thanks, I appreciate it
Peon

This topic is closed to new replies.

Advertisement