Did you know ... Search Documentation:
Pack canny_tudor -- prolog/canny/endian.pl
PublicShow source

The endian predicates unify big- and little-endian words, longs and long words with lists of octets by applying shifts and masks to correctly align integer values with their endian-specific octet positions. They utilise integer-relational finite domain CLP(FD) predicates in order to implement forward and reverse translation between octets and integers.

Use of CLP allows the DCG clauses to express the integer relations between octets and their integer interpretations implicitly. The constraints simultaneously define a byte in terms of an octet and vice versa.

 byte(?Byte:integer)// is semidet
Parses or generates an octet for Byte. Bytes are eight bits wide and unify with octets between 0 and 255 inclusive. Fails for octets falling outside this valid range.
Arguments:
Byte- value of octet.
 big_endian(?Width:integer, ?Word:integer)// is semidet
Unifies big-endian words with octets.

Example as follows: four octets to one big-endian 32-bit word.

?- phrase(big_endian(32, A), [4, 3, 2, 1]),
   format('~16r~n', [A]).
4030201
 little_endian(?Width:integer, ?Word:integer)// is semidet
Unifies little-endian words with octet stream.