對于目標(biāo)機(jī)是大端字節(jié)序的機(jī)器,進(jìn)行字節(jié)碼的轉(zhuǎn)換,提供了16byte、32byte、64byte字節(jié)的轉(zhuǎn)換。在intset\ziplist\zipmap三種數(shù)據(jù)結(jié)構(gòu)中使用,使得不同字節(jié)序機(jī)器生成的rdb文件格式都是統(tǒng)一的(小端字節(jié)序),便于兼容。
代碼實(shí)在是太簡單了,貼上來,不多說了。
endian.h
1 #ifndef __ENDIAN_H 2 #define __ENDIAN_H 3 4 void memrev16( void * p); 5 void memrev32( void * p); 6 void memrev64( void * p); 7 8 /* variants of the function doing the actual convertion only if the target 9 * host is big endian */ 10 #if (BYTE_ORDER == LITTLE_ENDIAN) 11 #define memrev16ifbe(p) 12 #define memrev32ifbe(p) 13 #define memrev64ifbe(p) 14 #else 15 #define memrev16ifbe(p) memrev16(p) 16 #define memrev32ifbe(p) memrev32(p) 17 #define memrev64ifbe(p) memrev64(p) 18 #endif 19 20 #endif
endian.c
1 /* Toggle the 16 bit unsigned integer pointed by *p from little endian to 2 * big endian */ 3 void memrev16( void * p) { 4 unsigned char *x = p, t; 5 6 t = x[ 0 ]; 7 x[ 0 ] = x[ 1 ]; 8 x[ 1 ] = t; 9 } 10 11 /* Toggle the 32 bit unsigned integer pointed by *p from little endian to 12 * big endian */ 13 void memrev32( void * p) { 14 unsigned char *x = p, t; 15 16 t = x[ 0 ]; 17 x[ 0 ] = x[ 3 ]; 18 x[ 3 ] = t; 19 t = x[ 1 ]; 20 x[ 1 ] = x[ 2 ]; 21 x[ 2 ] = t; 22 } 23 24 /* Toggle the 64 bit unsigned integer pointed by *p from little endian to 25 * big endian */ 26 void memrev64( void * p) { 27 unsigned char *x = p, t; 28 29 t = x[ 0 ]; 30 x[ 0 ] = x[ 7 ]; 31 x[ 7 ] = t; 32 t = x[ 1 ]; 33 x[ 1 ] = x[ 6 ]; 34 x[ 6 ] = t; 35 t = x[ 2 ]; 36 x[ 2 ] = x[ 5 ]; 37 x[ 5 ] = t; 38 t = x[ 3 ]; 39 x[ 3 ] = x[ 4 ]; 40 x[ 4 ] = t; 41 } 42 43 #ifdef TESTMAIN 44 #include <stdio.h> 45 46 int main( void ) { 47 char buf[ 32 ]; 48 49 sprintf(buf, " ciaoroma " ); 50 memrev16(buf); 51 printf( " %s\n " , buf); 52 53 sprintf(buf, " ciaoroma " ); 54 memrev32(buf); 55 printf( " %s\n " , buf); 56 57 sprintf(buf, " ciaoroma " ); 58 memrev64(buf); 59 printf( " %s\n " , buf); 60 61 return 0 ; 62 } 63 #endif
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
