bin2c(1)
π bin2c β embed data files into C as variables π
okay so bestie, welcome to bin2c(1) from the hxtools girlies β¨ this one is genuinely iconic and also lowkey underrated, so let's get into it.
πΈ SYNTAX πΈ
bin2c [-C program-file] [-D dir_prefix] [-H header-file] [-G guard-name]
[-p num] [-v] [--ultra] [--wxbitmap] file...
like, that's the whole vibe check. files go in, C code comes out. periodt. π
π¦ DESCRIPTION π¦
bin2c converts files to C variables. the way it does that is: it reads the contents of each file you hand it on the argument line, and then it writes out C program code that defines a variable holding that file's contents. no cap, that's the whole thing. π«Ά
and like, the hxtools version of bin2c? it's the main character, because it supports:
- space-efficient encoding of the arbitrary data β¨
- multiple input files (yes queen, plural, bring your whole friend group) π
- creation of either a standalone
.hfile, or a.c/.hfile pair. (peep the examples below, they explain it all.) π
β¨ OPTIONS β¨
-Cfile β if you specify this, the variable definition gets emitted to the filename you gave. that's it, that's the girl.-Ddir_prefix β every filename on the command line gets prefixed with the directory you pass. the implication here (and this is the important part, so listen up π«Ά) is that files will be searched relative to dir_prefix, while the variable name that ends up in the C source for an image stays non-prefixed. so the path changes, the variable name doesn't. iykyk.-Gname β you're requesting that an include guard with the identifier guard-name gets emitted into the header file. and heads up: if-Cwas specified and a.c/.hpair is generated, an include guard is always emitted, and then-Gis the option you use to set that name. π-Hfile β the file where the variable declarations get emitted to. if-Cwas not specified, the h-file will not contain any header guard by default, and it will contain the variable definition, the latter of which will be marked asstatic(file scope). okay so that's a whole thing, just roll with it.-pnum β strip num leading path components when transforming input paths to variable names. and if num is negative, that many trailing path components are retained instead. if-pisn't specified at all, the default is-1. π-vβ be verbose, bestie. show all the names and filenames thatbin2cwill write. not me wanting to see everything. π--ultraβ write out the raw data as a string literal. β οΈ but here's the catch and it's a real one: as the literal contains a trailing NUL byte, the size of the array is necessarily also one byte longer than the raw data, which you need to account for. (in C, it would be possible to writechar x[3] = "ABC", but this leads to an error in C++ wherex[4]is required.) so like, mind the gap, it's giving off-by-one energy π--wxbitmapβ generate C++ code that generateswxBitmapobjects. (this implies Ultra encoding, and deals with it appropriately, too.) fancy fr fr π
π EXAMPLES π
so if you want to generate a single C header file that behaves like XPM files and which is suitable for inclusion into a C program file of your own, you go:
bin2c -H images.h foo.png bar.png
and if you want a .c/.h pair instead, the invocation is:
bin2c -C images.c foo.png bar.png
this will create a C program file "images.c" containing all the definitions and a "images.h" that your code can use to get ahold of the declarations. two files, one slay. π
β¨
π HISTORY π
fun fact for the lore girlies: hxtools's bin2c developed from the earlier png2wx.pl utility. glow-up of the century honestly. π¦
π SEE ALSO π
and that's the whole page bestie!! this one came from hxtools and it's dated 2013-09-29 β thank you for coming to my ted talk, go embed some files, mwah ππ«Άβ¨π
π¦ where this page comes from
- debian bookworm: hxtools 20221119-1 Β· MIT
- debian trixie: hxtools 20231224-2+b2 Β· MIT
- opensuse-leap 16.0: hxtools-man 20231224-bp160.1.12 Β· GPL-2.0-or-later AND WTFPL
- ubuntu jammy: hxtools 20211204-1 Β· GPL-2.0-or-later OR GPL-2+3 OR WTFPL
- ubuntu noble: hxtools 20231224-2ubuntu1 Β· MIT
- ubuntu questing: hxtools 20231224-2ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms