copy
#include <IOSTREAM> //#include <WINSOCK.H> using std;
- typedef uint16;
- unsigned uint32;
- // 短整型大小端互换 #define BigLittleSwap16(A) ((((uint16)(A) & 0xff00) >> 8) | \ // 长整型大小端互换 #define BigLittleSwap32(A) ((((uint32)(A) & 0xff000000) >> 24) | \ // 本机大端返回1,小端返回0 int {
- unsigned i;
- s[4];
- }c;
- (0x12 == c.s[0]);
- }
- // 模拟htonl函数,本机字节序转网络字节序 t_htonl(unsigned h)
- {
- checkCPUendian() ? h : BigLittleSwap32(h);
- }
- // 模拟ntohl函数,网络字节序转本机字节序 t_ntohl(unsigned n)
- {
- checkCPUendian() ? n : BigLittleSwap32(n);
- }
- // 模拟htons函数,本机字节序转网络字节序 t_htons(unsigned h)
- {
- checkCPUendian() ? h : BigLittleSwap16(h);
- }
- // 模拟ntohs函数,网络字节序转本机字节序 t_ntohs(unsigned n)
- {
- checkCPUendian() ? n : BigLittleSwap16(n);
- }
- //8个二进制(2个十六进制)数转换成十进制数,不含a-f。即00-99的转换 int x)
- {
- y;
- temp;
- y+=temp*16;
- y;
- }
- //32个二进制(4个十六进制)数转换成十进制数,不含a-f。即0000-9999的转换 void x)
- {
- temp1=x%100;
- y1=transfer_0(temp1);
- temp2=(x%10000-temp1)/100;
- y2=transfer_0(temp2);
- temp3=(x%1000000-temp1-temp2*100)/10000;
- y3=transfer_0(temp3);
- temp4=(x%100000000-temp1-temp2*100-temp3*10000)/1000000;
- y4=transfer_0(temp4);
- ,y4,y3,y2,y1);
- //将16进制数数转化成10进制数,一位的0-F。 int x)
- y=0;
- (x>= && x<=)
- {
- ;
- y;
- (x>= && x<=)
- +10;
- y;
- }
- (x>= && x<=)
- {
- +10;
- y;
- );
- //将16进制数数转化成10进制数,八位的00000000-FFFFFFFF。 void x[], n)
- y0=transfer_0(x[0]);
- y1=transfer_0(x[1]);
- y2=transfer_0(x[2]);
- y3=transfer_0(x[3]);
- y4=transfer_0(x[4]);
- y5=transfer_0(x[5]);
- y6=transfer_0(x[6]);
- y7=transfer_0(x[7]);
- ,y0*16+y1,y2*16+y3,y4*16+y5,y6*16+y7);
- //将16进制数数转化成10进制数,八位的00000000-FFFFFFFF。 void x[])
- {
- y1=transfer_0(x[0])*16+transfer_0(x[1]);
- y2=transfer_0(x[2])*16+transfer_0(x[3]);
- y3=transfer_0(x[4])*16+transfer_0(x[5]);
- y4=transfer_0(x[6])*16+transfer_0(x[7]);
- ,y1,y2,y3,y4);
- }
- int ret;
- ret = checkCPUendian();
- (ret == 1)
- );
- );
- 0;
- }
IP地址转换、主机大小端、htonl、ntohl实现
时间: 2024-10-16 04:18:54