当前位置:网站首页>[network protocol] | [01] network byte order big end and small end

[network protocol] | [01] network byte order big end and small end

2022-06-09 02:24:00 Jxiepc

List of articles

1、 Byte order

 Is the byte order or sequence of digital data in computer memory , It can also be used to describe the order in which bits are transmitted over a communication channel ;
	- big-endian( Big end ): Store the most significant byte of a word in the smallest memory address , The least significant byte is stored in the largest memory address ;
	- little-endian( The small end ): Store the least significant byte at the least address ;

big-endian Is the most important sequence in network protocols ;
little-endian Is the main sequence of the processor architecture and its associated memory ;

The importance of byte order storage
 Insert picture description here

 Insert picture description here
Large end to small end conversion

template<class T>
typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::type
byteswap(T val) {
    
    return (T)bswap_64((uint64_t)val);
}

template<class T>
typename std::enable_if<sizeof(T) == sizeof(uint32_t), T>::type
byteswap(T val) {
    
    return (T)bswap_32((uint32_t)val);
}

template<class T>
typename std::enable_if<sizeof(T) == sizeof(uint16_t), T>::type
byteswap(T val) {
    
    return (T)bswap_16((uint16_t)val);
}

void test() {
    
	uint64_t x = 0x12345678;
	printf("%X\n", x);
	printf("%lx\n", byteswap(x));
}
原网站

版权声明
本文为[Jxiepc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090215309776.html