Archived

This topic is now archived and is closed to further replies.

Chomping charecter arrays

This topic is 5669 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 have a char* to a long string of numbers eg. "283820123920123928123" I need to write a function that returns a integer value in a group of two of the string of numbers. Eg. if I wanted value # 1 it would be 28 (using the above string example) or if I wanted value # 3 I would get 39. How do I do this???

Share this post


Link to post
Share on other sites
Your post makes no sense ... this obviously sounds like a class assignment ... but you don''t even define the parameters of the problem wll enough to make a solution. You don''t expalin WHY 28 would be #1 or why #3 would be 39. I assume it is the first and third two digit number which appears in the list twice ... but you definately do not say that. If english is not your first language, then I apologize.

Share this post


Link to post
Share on other sites
Ok sorry if the first post was confusing. It's not for a class assignment its for a small 2d tile engine I'm making.

Here's how it works. The map maker takes each tile in the map and gives it an index and then the whole map is saved in a linear format like this "01234523..." this would be decifered as tile #01, tile #23, tile #45, tile #23 ... now the tile engine reads that large string into a char array. I need a way to take each tile index (which are in sets of two, eg. tile 1 would be writed as 01 in the char array) and put them into a large int array.

how do I do this??

[edited by - Nullio on June 3, 2002 11:30:56 PM]

Share this post


Link to post
Share on other sites

int magic(const char *str, unsigned int thingy) {
thingy *= 2;
return (Cur[thingy]-'0')*10+(Cur[thingy+1]-'0');
}

May I suggest you just save the data in a binary format? Then, with two bytes, you can store values up to 65536 and read it more easily. Edit: typo.



[edited by - Null and Void on June 3, 2002 11:41:16 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I can''t remember exactly but try looking up strncpy and atoi. And just copy the two characters into a temp buffer and convert those to an integer value.

Kory

PS I''m not exactly sure about strncpy. Basically you want a function to copy an array of characters given a size.

Share this post


Link to post
Share on other sites
If you''re using standard C I/O just used fread/fwrite to read/write arrays of ''2 byte integers'' ("short" in most 32bit compilers). Make sure to through a ''b'' in the open flags though, to make sure the file is read in ''binary mode''. If you''re using standard C++ I/O include ios_base::binary in the open flags and use the read and write member functions to do the same thing as I said about fread/fwrite.

Share this post


Link to post
Share on other sites

  
//To write:

long TileIndices[NUM_TILES];
ofstream LevelDataW("tiledata.dat", ios_base::binary);
LevelDataW.write((char *)TileIndices, NUM_TILES*sizeof(long));
LevelDataW.close();

//To read:

long TileIndices[NUM_TILES];
ifstream LevelDataR("tiledata.dat", ios_base::binary);
LevelDataR.read((char *)TileIndices, NUM_TILES*sizeof(long));
LevelDataR.close();


Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
You should, of course, do the obligatory checks for the file''s existance before you attempt to read from it. Failure to do so may result in your program crashing. Here''s a brute force method:

bool FileExists(char *FileName)
{
ifstream X(FileName, ios::nocreate);
if(!X.bad())
{
X.close();
return true;
}
else
return false;
}

or you could just use the ios::nocreate flag when creating your input file stream (or when calling create, whichever is the case) and say something like

if(FileStream.fail())
{
MessageBox(0, "File doesn''t exist!", "Tile loading failed!", MB_OK);
// exit somehow
}

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As someone said, use a 2-byte primitive, like short.

char *c = "12345678";
short *s = (short*)c;

(s[0] & 0x00FF)-''0'' = 1;
(s[0] >> 8 )-''0'' = 2;
(s[1] & 0x00FF)-''0'' = 3;
(s[2] >> 8 )-''0'' = 4;

and so on...

when you get a negative number, you have reached the end ''\0''-''0'' = -48


Share this post


Link to post
Share on other sites
The easiest way is


  
// this function can convert a 2 digit DECIMAL number to an int (or short or char of course)

// no error checking done, assumes the pointer points to two valid digits

int TwoDigitsToInt(char *numString)
{
// subtracting 0x30 strips the ascii off the char

// the * 10 makes the first character the tens digit

return (numString[0] - 0x30) * 10 + (numString[1] - 0x30);
}

int GetNumberAt(char *numString, int numberIndex)
{
int byteIndex = numberIndex * 2;

if(strlen(numString < byteIndex + 2)
; // you have asked for an invalid number ...


return TwoDigitsToInt(numString + byteIndex);
// OR ... alternate systax

// return TwoDigitsToInt(&numString[byteIndex]);

}


I broke the problem down that way, so you can use whatever piece of the solution you need.

btw ... don''t go down the dark path of binary files yet, since then you get stupid cross-platform endian issues and such - for devlopment purposes "plain-text" is always better, because you can edit the file manually to add data when needed, until you know the code works right.

Share this post


Link to post
Share on other sites