tags:

views:

51

answers:

4

Hi, 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)

I try to do that in C++ by the way.

Thanks for your help.

+2  A: 

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.

Brian R. Bondy
Ok, thanks for the clarification. I think the problem I have here is to understand how to write a file in binary format (U3D is a binary format). I thought, I have to get my data, convert that to hex and write that with "file << std::hex << blablabla" but it seems that is not the case. Do you have references (links, simple tutorials) to see how a binary file is written?
draetsch
@draetsch: You just write out bytes of data like normal. For example if using C like API fopen with "wb" and if using C++ fstream specify the flags ,ios::out|ios::binary. You wouldn't use std::hex for this. That is for converting a number to a string hex representation. For additional questions from this I think you should post a new question, you'll get more eyes on it that way.
Brian R. Bondy
A: 

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.

Pavel Radzivilovsky
A: 

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.

Don Wakefield
A: 

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

uint8_t
int16_t
uint32_t
R Samuel Klatchko