upvote
Yeah xxd is the correct answer. If you don't want to install a dependency and have Lua installed, or if you're just feeling a little bit frisky, you can use my function which is Production Ready™.

    Xxd = function(name, input)
        if not name:find'^[_%a][_%w]*$' then error('bad name: '..tostring(name)) end
        local ans = {
            'const unsigned int '..name..'_len = '..(#input)..';',
            'const unsigned char '..name..'[] = {',
        }
        local t = {}
        for i=1,#input do
            table.insert(t, ('0x%02x,'):format(input:byte(i)))
            if #t == 16 then -- 16 columns per row. arbitrary, change this if you want
                table.insert(ans, table.concat(t))
                t = {}
            end
        end
        if #t ~= 0 then
            table.insert(ans, table.concat(t))
        end
        table.insert(ans, '};\n')
        return table.concat(ans, '\n')
    end
I am distributing it under the terms of the GNU GPL v3. So if you put this in your codebase I will sue you into releasing your entire source. Just kidding it's MIT licensed.

Honestly that's a terrible joke. Seriously it's MIT. Here I will put the full license in this comment to illustrate how serious I am:

Copyright 2026 rweichler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

reply
Feels like Lua is a more exotic dependency. I used to use xxd but this gets problematic when files grow (and they don't need to grow much at all), objcopy is much faster (which I didn't think would matter much, but it did) and don't have the same issues that accidentally opening the .h file in your editor or code-searching having to traverse that mess.

Yes, you'd want to gitignore it and exclude it from search etc. but you still have size issues etc. See gucci-on-fleek's comment on objcopy above for usage.

reply