Question

I am currently trying to learn some more in depth stuff of file formats.

I have a spec for a 3D file format (U3D in this case) and I want to try to implement that. Nothing serious, just for the learning effect.

My problem starts very early with the types, that need to be defined. I have to define different integers (8Bit, 16bit, 32bit unsigned and signed) and these then need to be converted to hex before writing that to a file.

How do I define these types, since I can not just create an I16 i.e.? Another problem for me is how to convert that I16 to a hex number with 8 digits (i.e. 0001 0001).

Was it helpful?

Solution

Hex is just a representation of a number. Whether you interpret the number as binary, decimal, hex, octal etc is up to you. In C++ you have support for decimal, hex, and octal representations, but they are all stored in the same way.

Example:

int x = 0x1;
int y = 1;
assert(x == y);

Likely the file format wants you to store the files in normal binary format. I don't think the file format wants the hex numbers as a readable text string. If it does though then you could use std::hex to do the conversion for you. (Example: file << hex << number;)

If the file format talks about writing more than a 1 byte type to file then be careful of the Endianness of your architecture. Which means do you store the most significant byte of the multi byte type first or last.

It is very common in file format specifications to show you how the binary should look for a given part of the file. Don't confuse this though with actually storing binary digits as strings. Likewise they will sometimes give a shortcut for this by specifying in hex how it should look. Again most of the time they don't actually mean text strings.

The smallest addressable unit in C++ is a char which is 1 byte. If you want to set bits within that byte you need to use bitwise operators like & and |. There are many tutorials on bitwise operators so I won't go into detail here.

OTHER TIPS

If you include <stdint.h> you will get types such as:

uint8_t
int16_t
uint32_t

First, let me understand. The integers are stored AS TEXT in a file, in hexadecimal format, without prefix 0x?

Then, use this syntax:

fprintf(fp, "%08x", number);

Will write 0abc1234 into a file.

As for "define different integers (8Bit, 16bit, 32bit unsigned and signed)", unless you roll your own, and concomitant math operations for them, you should stick to the types supplied by your system. See stdint.h for the typedefs available, such as int32_t.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top