使用共用体检测系统大小端
# include <stdio.h>
# include <stdint.h>
union EndiannessChecker {
uint32_t intValue;
uint8_t byteArray[ 4 ] ;
} ;
int main ( ) {
union EndiannessChecker checker;
checker. intValue = 0x12345678 ;
if ( checker. byteArray[ 0 ] == 0x78 ) {
printf ( "系统采用小端字节序(Little Endian)\n" ) ;
}
else if ( checker. byteArray[ 0 ] == 0x12 ) {
printf ( "系统采用大端字节序(Big Endian)\n" ) ;
}
else {
printf ( "无法确定系统字节序\n" ) ;
}
return 0 ;
}
使用memcpy进行数组数据拼接
# include <stdio.h>
# include <string.h>
int main ( ) {
int arr1[ ] = { 1 , 2 , 3 } ;
int arr2[ ] = { 4 , 5 , 6 } ;
int len1 = sizeof ( arr1) / sizeof ( arr1[ 0 ] ) ;
int len2 = sizeof ( arr2) / sizeof ( arr2[ 0 ] ) ;
int result[ len1 + len2] ;
memcpy ( result, arr1, len1 * sizeof ( int ) ) ;
memcpy ( result + len1, arr2, len2 * sizeof ( int ) ) ;
for ( int i = 0 ; i < len1 + len2; i++ ) {
printf ( "%d " , result[ i] ) ;
}
return 0 ;
}
# include <stdio.h>
# include <stdint.h>
int main ( ) {
uint8_t array1[ 4 ] = { 0x12 , 0x34 , 0x56 , 0x78 } ;
uint32_t result = 0 ;
for ( int i = 0 ; i < 4 ; i++ ) {
result = ( result << 8 ) | array1[ i] ;
}
printf ( "拼接后的32位整数:0x%08x\n" , result) ;
return 0 ;
}
多机大小端匹配
# include <stdio.h>
# include <stdint.h>
# include <arpa/inet.h>
int main ( ) {
uint32_t hostValue = 0x12345678 ;
uint32_t networkValue = htonl ( hostValue) ;
printf ( "Network Byte Order: 0x%08x\n" , networkValue) ;
uint32_t hostValueRestored = ntohl ( networkValue) ;
printf ( "Host Byte Order Restored: 0x%08x\n" , hostValueRestored) ;
return 0 ;
}
有些编译器不支持网络编程,以下为htohl与ntohl函数实现
# include <stdint.h>
# include <stdio.h>
uint32_t htonl ( uint32_t hostlong) {
uint32_t networklong = 0 ;
networklong |= ( hostlong & 0xFF ) << 24 ;
networklong |= ( ( hostlong >> 8 ) & 0xFF ) << 16 ;
networklong |= ( ( hostlong >> 16 ) & 0xFF ) << 8 ;
networklong |= ( hostlong >> 24 ) & 0xFF ;
return networklong;
}
uint32_t ntohl ( uint32_t netlong) {
uint32_t hostlong = 0 ;
hostlong |= ( netlong & 0xFF ) << 24 ;
hostlong |= ( ( netlong >> 8 ) & 0xFF ) << 16 ;
hostlong |= ( ( netlong >> 16 ) & 0xFF ) << 8 ;
hostlong |= ( netlong >> 24 ) & 0xFF ;
return hostlong;
}
int main ( ) {
uint32_t hostValue = 0x12345678 ;
uint32_t networkValue = htonl ( hostValue) ;
printf ( "Network Byte Order: 0x%08x\n" , networkValue) ;
uint32_t hostValueRestored = ntohl ( networkValue) ;
printf ( "Host Byte Order Restored: 0x%08x\n" , hostValueRestored) ;
return 0 ;
}
共用体进行大小端切换
# include <stdio.h>
union EndianConverter {
unsigned int integer;
unsigned char bytes[ 4 ] ;
} ;
int main ( ) {
union EndianConverter converter;
converter. integer = 0x12345678 ;
printf ( "原始数据:0x%08X\n" , converter. integer) ;
unsigned int bigEndianValue =
( converter. bytes[ 0 ] << 24 ) |
( converter. bytes[ 1 ] << 16 ) |
( converter. bytes[ 2 ] << 8 ) |
converter. bytes[ 3 ] ;
printf ( "切换为Big Endian:0x%08X\n" , bigEndianValue) ;
converter. integer =
( bigEndianValue >> 24 ) |
( ( bigEndianValue >> 16 ) & 0xFF00 ) |
( ( bigEndianValue >> 8 ) & 0xFF0000 ) |
( bigEndianValue << 24 ) ;
printf ( "切换回Little Endian:0x%08X\n" , converter. integer) ;
return 0 ;
}