Show HN: Hexi, modern header-only network binary serialisation for C++ hackers

https://news.ycombinator.com/rss Hits: 23
Summary

Hexi is a lightweight, header-only C++23 library for safely handling binary data from arbitrary sources (but primarily network data). It sits somewhere between manually memcpying bytes from network buffers and full-blown serialisation libraries. The design goals are ease of use, safety when dealing with untrusted data, a reasonable level of flexibility, and keeping overhead to a minimum. What Hexi doesn't offer: versioning, conversion between different formats, handling of text-based formats, unloading the dishwasher. Incorporating Hexi into your project is simple! The easiest way is to simply copy hexi.h from single_include into your own project. If you'd rather only include what you use, you can add include to your include paths or incorporate it into your own CMake project with target_link_library . To build the unit tests, run CMake with ENABLE_TESTING . Here's what some libraries might call a very simple motivating example: # include < hexi.h > # include < array > # include < vector > # include < cstddef > struct UserPacket { uint64_t user_id; uint64_t timestamp; std::array< uint8_t , 16 > ipv6; }; auto deserialise (std::span< const char > network_buffer) { hexi::buffer_adaptor adaptor (network_buffer); // wrap the buffer hexi::binary_stream stream (adaptor); // create a binary stream // deserialise! UserPacket packet; stream >> packet; return packet; } auto serialise ( const UserPacket& packet) { std::vector< uint8_t > buffer; hexi::buffer_adaptor adaptor (buffer); // wrap the buffer hexi::binary_stream stream (adaptor); // create a binary stream // serialise! stream << packet; return buffer; } By default, Hexi will try to serialise basic structures such as our UserPacket if they meet requirements for being safe to directly copy the bytes. Now, for reasons of portability, it's not recommended that you do things this way unless you're positive that the data layout is identical on the system that wrote the data. Not to worry, this is easily solved. Plus, we didn...

First seen: 2025-03-28 18:25

Last seen: 2025-03-29 18:29