Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Input/output and explicit binary serialization

Input/output transfers data, while serialization defines its representation. Learn how to encode an integer in little-endian order, decode it safely, and supplement round-trip validation with a known-byte check.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

serialization: value -> specified bytes; deserialization: validated bytes -> value; round-trip validation: deserialize(serialize(value)) == value

Input/output (I/O) transfers data between a program and its environment. Serialization converts values into an agreed representation, while deserialization reconstructs them after checking the input. C++ streams support reading and writing; transferring bytes alone does not define a data format.

A format should specify field widths, byte order, and validation rules. Little-endian byte order places the least significant byte first: 0x1234 is encoded as 0x34, 0x12. The example encodes one unsigned 16-bit integer in exactly two 8-bit bytes. Shifts and masking make the format independent of the machine's byte order; it requires CHAR_BIT == 8 and availability of the optional type std::uint16_t.

Dumping an object's raw memory is not a portable format: its representation can contain padding bits and depend on the implementation, and pointers do not store their pointed-to data. Permission to copy the bytes of trivially copyable types does not guarantee compatible files across platforms. Binary file mode does not standardize object representation either.

Round-trip validation checks deserialize(serialize(value)) == value. The example also compares the bytes against a known result, because two incorrect algorithms can cancel each other's mistakes. The decoder rejects lengths other than two before accessing the data; every two-byte sequence is a valid value here. Larger formats also need range, length, and version checks, plus I/O error handling.

The code is intended for compilation with a C++23 flag. The displayed source was run unchanged in an isolated environment, and its output matched the stated result exactly. That run does not verify execution in a production environment. The eel.is source is an evolving working draft, not a frozen C++23 publication.

Examples

#include <array>
#include <climits>
#include <cstdint>
#include <iostream>
#include <optional>
#include <span>

static_assert(CHAR_BIT == 8);

std::array<unsigned char, 2> serialize(std::uint16_t value) {
    return {static_cast<unsigned char>(value & 0xffu),
            static_cast<unsigned char>(value >> 8)};
}

std::optional<std::uint16_t> deserialize(
    std::span<const unsigned char> bytes) {
    if (bytes.size() != 2) return std::nullopt;
    return static_cast<std::uint16_t>(
        static_cast<unsigned int>(bytes[0]) |
        (static_cast<unsigned int>(bytes[1]) << 8));
}

int main() {
    const std::uint16_t original = 0x1234;
    const auto bytes = serialize(original);
    const auto restored = deserialize(bytes);
    const bool round_trip = restored && *restored == original;
    const bool known_bytes = bytes[0] == 0x34 && bytes[1] == 0x12;
    const bool rejected = !deserialize(
        std::span<const unsigned char>{bytes.data(), 1});

    std::cout << "bytes: " << static_cast<unsigned int>(bytes[0])
              << ' ' << static_cast<unsigned int>(bytes[1]) << '\n'
              << std::boolalpha
              << "round-trip: " << round_trip << '\n'
              << "known bytes: " << known_bytes << '\n'
              << "invalid length rejected: " << rejected << '\n';
    return round_trip && known_bytes && rejected ? 0 : 1;
}

Result

bytes: 52 18
round-trip: true
known bytes: true
invalid length rejected: true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources