Skip to content

net.conv #

Description

net.conv provides a convenient way to convert number values to, and from the network byte order format (which is always big endian).

When communicating across a network, it is possible that the machines use different byte orders, since the host format of each system can vary, depending on the CPU, and on most systems, is usually little endian.

To avoid mismatches due to that, the network byte order is used by convention to send network data in a manner that will be received coherently, regardless of the endianness of the sender system and the receiver system.

fn hton16 #

fn hton16(host u16) u16

hton16 converts the 16 bit value host to the net format (htons)

fn hton32 #

fn hton32(host u32) u32

hton32 converts the 32 bit value host to the net format (htonl)

fn hton64 #

fn hton64(host u64) u64

hton64 converts the 64 bit value host to the net format (htonll)

fn htonf32 #

fn htonf32(host f32) f32

htonf32 converts the 32 bit double host to the net format

fn htonf64 #

fn htonf64(host f64) f64

htonf64 converts the 64 bit double host to the net format

fn ntoh16 #

fn ntoh16(net u16) u16

ntoh16 converts the 16 bit value net to the host format (ntohs)

fn ntoh32 #

fn ntoh32(net u32) u32

ntoh32 converts the 32 bit value net to the host format (ntohl)

fn ntoh64 #

fn ntoh64(net u64) u64

ntoh64 converts the 64 bit value net to the host format (ntohll)

fn reverse_bytes_u16 #

fn reverse_bytes_u16(a u16) u16

reverse_bytes_u16 reverse a u16's byte order

fn reverse_bytes_u32 #

fn reverse_bytes_u32(a u32) u32

reverse_bytes_u32 reverse a u32's byte order

fn reverse_bytes_u64 #

fn reverse_bytes_u64(a u64) u64

reverse_bytes_u64 reverse a u64's byte order

fn u64tovarint #

fn u64tovarint(n u64) ![]u8

u64tovarint converts the given 64 bit number n, where n < 2^62 to a byte array, using the variable length unsigned integer encoding from: https://datatracker.ietf.org/doc/html/rfc9000#section-16 . The returned array length .len, will be in [1,2,4,8] .

fn varinttou64 #

fn varinttou64(b []u8) !(u64, u8)

varinttou64 parses a variable length number from the start of the given byte array b. If it succeeds, it returns the decoded number, and the length of the parsed byte span.