Byte sorting function, address Conversion Function

Source: Internet
Author: User
Tags htons

Byte conversion function:


The existence of the byte conversion function is mainly because there are two different byte orders: large and small.

To ensure normal communication, some data needs to be converted.


#include <netinet/in.h>uint16_t htons(uint16_t host16bitvalue)uint32_t htonl(uint16_t host32bitvalue)

These two functions return the network order value;


uint16_t ntohs(uint16_t host16bitvalue)uint32_t ntohl(uint16_t host32bitvalue)

These two functions return the host sequence values;


The specific functions of these functions can be memorized by the function name without memorizing them.

Here, H represents host; n represents network; s represents short, which is 16 bits, for example, port number; l represents long, which is 32 bits, for example, IP address;

For example, the role of htons is the transformation of host to network, and it is 16 bits.



Address conversion function:


The address conversion function converts IP addresses between the decimal and 32-bit binary numbers.


#include <arpa/inet.h>int inet_pton(int af, const char *src, void *dst)

This function is used to convert the string (Dot-decimal number) to a 32-bit binary number.

You can also use the function name to remember the specific function. P represents presentation, and N represents numeric. Pton is transformed from presentation to numeric.

The first parameter AF is the address family, which exists in DST after conversion.

Inet_ton is an extension of inet_addr. The following multi-address families are supported:

Af_inet: SRC is the first address (DDD. ddd. ddd. the function converts the address to the in_addr struct and copies it in * DST.

Af_inet6: RC indicates the IPv6 address. The function converts the address to the in6_addr struct and copies it in * DST.

If a function error occurs, a negative value is returned and errno is set to eafnosupport. If the address family specified by the AF parameter and SRC format are incorrect, the function returns 0.

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)

You can also use the function name to remember the specific function. P represents presentation, and N represents numeric. Ntop is used to transform from numeric to presentation.

This function converts 32-bit binary to a string (Point-to-decimal)

The function of the parameter is the same as that above, but there is an additional parameter socklen_t CNT, which is the size of the DST pointing to the cache area to avoid overflow. If the cache area is too small, the address value cannot be stored, returns a null pointer and sets errno to enospc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.