In the history of computer development, there has been no unified standard for data storage
There are two common rules for byte ordering. For example, little-endian refers to arranging the lower digits of a multi-digit number at smaller addresses and the higher digits at larger addresses; conversely, big-endian is when this arrangement is reversed. Byte order is a factor that must be considered in network applications because different machine types may use different standards, so they are all converted according to the network standard.
Big-endian byte order aligns better with the left-to-right reading habit
Please provide the Chinese text you want me to translate. I am ready when you are! Just paste the text here.
- Processors such as x86, MOS Technology 6502, Z80, VAX, and PDP-11 are little-endian
- Processors such as Motorola 6800, Motorola 68000, and PowerPC 970 use big-endian order
- The byte order for ARM, PowerPC (excluding PowerPC 970), DEC Alpha, SPARC V9, MIPS, PA-RISC, and IA64 is configurable
Network Preface
Network transmission generally uses big-endian order, also known as network byte order or network order. Big-endian is defined as the network byte order in the IP protocol. Socket definitions specify a set of conversion functions for transforming 16- and 32-bit integers between network byte order and host byte order
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong); //把uint32_t类型从主机序转换到网络序
uint16_t htons(uint16_t hostshort); //把uint16_t类型从主机序转换到网络序
uint32_t ntohl(uint32_t netlong); //把uint32_t类型从网络序转换到主机序
uint16_t ntohs(uint16_t netshort); //把uint16_t类型从网络序转换到主机序
If INLIN_ITALIC_1__ is chosen as the network library, the built-in namespace contains cross-platform adapted function names
- boost::asio::detail::socket_ops::network_to_host_long
- boost::asio::detail::socket_ops::network_to_host_short
- boost::asio::detail::socket_ops::host_to_network_long
- boost::asio::detail::socket_ops::host_to_network_short
Visual Studio debugger
In debug mode, select the Debug menu, then Window, and check Memory Window
In INLIN_ITALIC_1__, you can view data in memory directly within the debugger, as shown in the figure below
Please provide the Chinese text you want me to translate. I am ready when you are.
How to check memory
- Prints the variable name and jumps to its address
- If the variable is already a pointer, select it by double-clicking, drag it to the memory window, and display the contents at that address
- If the variable is not a pointer, add it to the calculation window, obtain its address, then manually copy it to the memory window
For example
Received data, stored in INLINE_CODE_0__对象中,将网络序转成主机序,得到__INLINE_CODE_1 equal to 30, and the server allocates four bytes for transmitting this data
bool NetworkMessage::decode_header()
{
// 网络序转成主机序
body_length_ = boost::asio::detail::socket_ops::network_to_host_long(*(int *)buffer_.data());
return auto_reserve(body_length_);
}
Observe the content of INLINE_CODE_0 in the memory window
Observe the content of INLINE_CODE_0 in the memory window