Archived

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

Chomping charecter arrays

This topic is 6011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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.

very carefully.

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 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 on other sites
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 on other sites
Null and Void could you elaborate on your binaray storage technique?

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 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.//

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 15
• Forum Statistics

• Total Topics
633392
• Total Posts
3011639
• Who's Online (See full list)

There are no registered users currently online

×