type conversion during binary file read/write

Here's the code of a list of objects containing 6 sides and a reference (by ID) to the object next to each side (like a beehive). This chunk of code extract the 6 neighbor IDs (which are of type unsigned int) from each object and output each ID to a binary file (using char elements). So basically it's purpose is to translate each byte of each unsigned int to a char and put all the chars together in a memblock to output to the file. The second block of code does the opposite; extract each neighbor ID from the file and copy them to each object in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const unsigned short NUMBER_OF_DIRECTIONS = 6;
const unsigned short NUMBER_OF_BYTES_IN_UNSIGNED_INT = 4;

fstream map_template("map_template.bin", ios::out | ios::binary);

unsigned int total_number, partial_number[3], memblock_index = 0;
	
unsigned int memblock_size = number_of_cells*NUMBER_OF_BYTES_IN_UNSIGNED_INT*NUMBER_OF_DIRECTIONS;

char* memblock = new char[memblock_size];

actual_cell = tail;

direction = enum_namespace::southeast;

while(actual_cell)
{
	for(unsigned short i = 0; i < NUMBER_OF_DIRECTIONS; i++, next_direction(direction))
	{
		total_number = actual_cell->get_direction(direction);
		
		partial_number[0] = total_number >> 24;
		partial_number[1] = total_number >> 16;
		partial_number[2] = total_number >> 8;
		
		memblock[memblock_index] = partial_number[0];
		memblock_index++;
			
		memblock[memblock_index] = partial_number[1] ^ partial_number[0] << 8;
		memblock_index++;
		
		memblock[memblock_index] = partial_number[2] ^ partial_number[1] << 8;
		memblock_index++;
		
		memblock[memblock_index] = total_number ^ partial_number[2] << 8;
		memblock_index++;
	}
	
	actual_cell = actual_cell->next;
}

map_template.write(memblock, memblock_size);
map_template.close();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const unsigned short NUMBER_OF_DIRECTIONS = 6;
const unsigned short NUMBER_OF_BYTES_IN_UNSIGNED_INT = 4;

unsigned int total_number, memblock_index = 0, memblock_size = number_of_cells*NUMBER_OF_BYTES_IN_UNSIGNED_INT*NUMBER_OF_DIRECTIONS;

char* memblock = new char[memblock_size];

fstream map_template("map_template.bin", ios::in | ios::binary);

map_template.read(memblock, memblock_size);
map_template.close();

actual_cell = tail;

direction = enum_namespace::southeast;

while(actual_cell)
{
	for(unsigned short i = 0; i < NUMBER_OF_DIRECTIONS; i++, next_direction(direction))
	{
		total_number = memblock[memblock_index] << 24;
		memblock_index++;
		
		total_number += memblock[memblock_index] << 16;
		memblock_index++;
		
		total_number += memblock[memblock_index] << 8;
		memblock_index++;
		
		total_number += memblock[memblock_index];
		
		actual_cell->set_direction(direction, total_number);
	}
	
	actual_cell = actual_cell->next;
}


My question is, since I'm interested in the bitwise information of each byte in the unsigned int's, are the bytes going to be modified during the conversions from unsigned int to char and from char to unsigned int, or are all the bits going to stay the same?

Thank you,
Aeonflux1212
Last edited on
Topic archived. No new replies allowed.