Home

be16toh

be16toh is a macro or inline function used in C and C++ programs to convert a 16-bit value from big-endian byte order to the host machine’s native byte order. It is the inverse of htobe16 and is commonly grouped with other endian conversion utilities such as be32toh and be64toh. The name reflects its purpose: transforming data stored in big-endian format (often referred to as network byte order) into the host’s endianness.

Availability and portability: On POSIX-like systems, be16toh is typically defined in endian.h or sys/endian.h and is

Usage: be16toh is used when reading binary data that is specified as big-endian, ensuring that the value

Example:

#include <endian.h>

uint16_t net = 0x1234; // big-endian data

uint16_t host = be16toh(net);

Implementation notes: Implementations may use conditional compilation to select a fast byte-swap intrinsic or a portable

See also: htobe16, be32toh, be64toh, be16toh’s sibling functions provide host-to-big-endian conversions. If a platform lacks the

provided
alongside
related
macros
like
be32toh,
be64toh,
and
their
host-to-big-endian
counterparts.
Implementations
generally
rely
on
compile-time
checks
of
the
host’s
byte
order.
If
the
host
is
big-endian,
be16toh
is
typically
a
no-op;
if
little-endian,
it
performs
a
16-bit
byte
swap.
is
interpreted
correctly
on
the
host.
For
example,
when
parsing
a
protocol
field
or
a
binary
file
format
that
stores
values
in
network
byte
order,
one
would
convert
each
16-bit
field
to
host
order
before
further
processing.
shift-based
swap.
On
big-endian
hosts,
be16toh
may
simply
return
the
input
value;
on
little-endian
hosts,
it
swaps
the
two
bytes.
macro,
manual
byte-swapping
or
platform-specific
equivalents
can
be
used.