Advertisement

file loading and dec to hex conversion

Started by March 07, 2002 09:03 AM
0 comments, last by werdy666 22 years, 11 months ago
ok i am loading in a mappy .map file and the first 4 bytes tell me how big the map is. the first 2 bytes are how many blocks across, and the second 2 bytes are how many blocks down. I load the first 4 bytes of the file into a char array. So I might have pos[0 to 4] now lets say across is 300 blocks, checking the map file in a hex reader it says for the first 2 bytes 2c 01. Now i load these two values into pos[0] and pos[1] as chars. When they are loaded in they convert back to decimal numbers. I know i can convert a char into a int or float or whatever, but i am having trouble working out or finding somewhere that can teach me how to put both of the 2 bytes together the right way round and convert the 01 2c into a decimal number which in turn will give the info for my drawing routines for how many blocks to draw and also my map loading routine to load the specific amount of bytes. I hope you can understand what i mean. Any help would be greatly appreciatted. I am just trying a basic 2d tile engine in opengl and want to make the engine load any sized map, just not sure how to do it with 2 bytes back to front! Thanks for your help werdy666 i should not code at 2am! lol
Using your way you could write

    unsigned char va[4];unsigned long val = (va[0]<<24)|(va[1]<<16)|(va[2]<<8)|va[3];  


but it would probably be easier to do this:

  unsigned long val;FILE *file = fopen("filename.bin","rb")if(file) {  if(fread(&val,4,1,file)!=1)   {    // load failed.   }  fclose(file); }  


[EDIT:]
Changed char to unsigned char.

I suggest the above for reading floats and doubles in binary. The other way would be very hard.

Edited by - smart_idiot on March 7, 2002 10:18:22 AM
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement