This utility helps You to keep and edit Your binary data in easy-to-edit XML form. The principle is simple:
...in a "definition" XML file. You specify what structures there are, what members they contain and how are the members encoded into binary.
My file consists of sequence of structures, each of which contains one-byte id, then three bytes with transparency RGB, then padding to 16 bytes followed by 48 chars long filename padded with zeroes and after all the records there is one terminating empty record etc.
You can define signedness and endianness for integer types, paddings for string types, default values for unspecified data, You may forbid specifying some data (see nouser="nouser" in XMLs below) etc.
Example:
types.xml: (defines some basic data types)
<types>
<typedef name="uint8" type="int" size="1" />
<typedef name="uint16" type="int" size="2" />
<typedef name="uint24" type="int" size="3" />
<typedef name="uint32" type="int" size="4" />
</types>
types-graphics.xml: (defines custom structures and data types)
<types>
<include type="xml" file="types.xml" />
<typedef name="surface-item" type="struct">
<item name="id" type="uint8" default="0" />
<item name="r" type="uint8" default="0" />
<item name="g" type="uint8" default="0" />
<item name="b" type="uint8" default="0" />
<item name="padding" type="string" size="12" padstr="$00" default="" nouser="nouser" />
<item name="file" type="string" size="48" padstr="$00" default="" />
</typedef>
<typedef name="root" type="struct">
<item name="surface" type="array" item="surface-item" />
<item name="surface-close" type="surface-item" nouser="nouser" />
</typedef>
</types>
.
...in human-readable and -writeable "data" XML file, structured as described in "definition" XML file:
data.xml:
<data types="types-graphics.xml" roottype="root">
<surface>
<id>1</id>
<file>rocket.png</file>
<!-- now goes the optional transparency color -->
<r>255</r>
<g>0</g>
<b>255</b>
</surface>
<surface>
<id>2</id>
<file>star.png</file>
<!-- ommited structure items get default values as defined in the datatype -->
</surface>
</data>
~~~~~~~
.
3. Generate the binary file
---------------------------
Whenever You need, You run the utility to convert the "data" XML file into **raw binary** using the "definition" XML file. This may be for example run as a post-build step of Your project etc.
php data2bin.php data.xml -o data.bin
.
4. Enjoy Your binary file
-------------------------
0010: 01 ff 00 ff 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0020: 72 6f 63 6b 65 74 2e 70 6e 67 00 00 00 00 00 00 | rocket.png......
0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0050: 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0060: 73 74 61 72 2e 70 6e 67 00 00 00 00 00 00 00 00 | star.png........
0070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
~~~~~~
The idea of quickly generating binary files using structures defined via XML is quite powerful and practical. Especially for developers working with binary formats and managing configurations or data packets at a low level, being able to obtain the output directly as binary while maintaining XML readability saves significant time. It can also be useful to quickly see the binary equivalent of the text during the structure definition phase or when checking the accuracy of the generated data. Simple tools that don't require extra installation make the process easier in this regard. I sometimes use this text-to-binary converter for such checks. The wiki content and general approach are sufficiently helpful for first-time users of the tool. A useful project that makes binary file generation more accessible; thanks to everyone involved.
Last edit: alida 2026-01-15