IP地址转换、主机大小端、htonl、ntohl实现

#include <IOSTREAM>
//#include <WINSOCK.H>
using namespace std;

typedef unsigned short int uint16;
typedef unsigned long int uint32;

// 短整型大小端互换
#define BigLittleSwap16(A)  ((((uint16)(A) & 0xff00) >> 8) | \
(((uint16)(A) & 0x00ff) << 8))
// 长整型大小端互换

#define BigLittleSwap32(A)  ((((uint32)(A) & 0xff000000) >> 24) | \
	(((uint32)(A) & 0x00ff0000) >> 8) | \
	(((uint32)(A) & 0x0000ff00) << 8) | \
(((uint32)(A) & 0x000000ff) << 24))

// 本机大端返回1,小端返回0
int checkCPUendian()
{
	union{
		unsigned long int i;
		unsigned char s[4];
	}c;
	
	c.i = 0x12345678;
	return (0x12 == c.s[0]);
}

// 模拟htonl函数,本机字节序转网络字节序
unsigned long int t_htonl(unsigned long int h)
{
	// 若本机为大端,与网络字节序同,直接返回
	// 若本机为小端,转换成大端再返回
	return checkCPUendian() ? h : BigLittleSwap32(h);
}

// 模拟ntohl函数,网络字节序转本机字节序
unsigned long int t_ntohl(unsigned long int n)
{
	// 若本机为大端,与网络字节序同,直接返回
	// 若本机为小端,网络数据转换成小端再返回
	return checkCPUendian() ? n : BigLittleSwap32(n);
}

// 模拟htons函数,本机字节序转网络字节序
unsigned short int t_htons(unsigned short int h)
{
	// 若本机为大端,与网络字节序同,直接返回
	// 若本机为小端,转换成大端再返回
	return checkCPUendian() ? h : BigLittleSwap16(h);
}

// 模拟ntohs函数,网络字节序转本机字节序
unsigned short int t_ntohs(unsigned short int n)
{
	// 若本机为大端,与网络字节序同,直接返回
	// 若本机为小端,网络数据转换成小端再返回
	return checkCPUendian() ? n : BigLittleSwap16(n);
}

//8个二进制(2个十六进制)数转换成十进制数,不含a-f。即00-99的转换   
int transfer_0(int x)  
{  
    int y;//结果   
    int temp;//临时值   
	
    y=x%10;//个位数   
    temp=(x%100-y)/10;//十位数   
    y+=temp*16;  
    return y;  
}  

//32个二进制(4个十六进制)数转换成十进制数,不含a-f。即0000-9999的转换   
void transfer_1(unsigned int x)  
{  
    //从右向左   
    int temp1=x%100;  
    int y1=transfer_0(temp1);  
	
    int temp2=(x%10000-temp1)/100;  
    int y2=transfer_0(temp2);  
	
    int temp3=(x%1000000-temp1-temp2*100)/10000;  
    int y3=transfer_0(temp3);  
	
    int temp4=(x%100000000-temp1-temp2*100-temp3*10000)/1000000;  
    int y4=transfer_0(temp4);  
	
    printf("结果是:%d.%d.%d.%d\n",y4,y3,y2,y1);  
}

//将16进制数数转化成10进制数,一位的0-F。   
int transfer_0(char x)  
{  
    int y=0;//返回值   
    if (x>='0' && x<='9')//0-9的数字   
    {  
        y=x-'0';  
        return y;  
    }  
    if (x>='a' && x<='f')//a-f的字母   
    {  
        y=x-'a'+10;  
        return y;  
    }  
    if (x>='A' && x<='F')//A-F的字母   
    {  
        y=x-'A'+10;  
        return y;  
    }  
    printf("参数错误!");  
    exit(1);  
}


//将16进制数数转化成10进制数,八位的00000000-FFFFFFFF。   
void transfer_3(char x[],int n)//长度为8   
{  
    //从左往右   
    int y0=transfer_0(x[0]);  
    int y1=transfer_0(x[1]);  
    int y2=transfer_0(x[2]);  
    int y3=transfer_0(x[3]);  
    int y4=transfer_0(x[4]);  
    int y5=transfer_0(x[5]);  
    int y6=transfer_0(x[6]);  
    int y7=transfer_0(x[7]);  
	
    printf("结果是:%d.%d.%d.%d\n",y0*16+y1,y2*16+y3,y4*16+y5,y6*16+y7);      
}


//将16进制数数转化成10进制数,八位的00000000-FFFFFFFF。   
void transfer_4(char x[])  
{  
    //从左往右   
    int y1=transfer_0(x[0])*16+transfer_0(x[1]);  
    int y2=transfer_0(x[2])*16+transfer_0(x[3]);  
    int y3=transfer_0(x[4])*16+transfer_0(x[5]);  
    int y4=transfer_0(x[6])*16+transfer_0(x[7]);  
	
    printf("结果是:%d.%d.%d.%d\n",y1,y2,y3,y4);      
}  


int main()
{
	int ret;
	ret = checkCPUendian();

	if (ret == 1)
	{
		printf("返回1,本机为大端\n");
	}
	else
	{
		printf("返回0,本机为小端\n");
	}
	
	return 0;
}

你可能感兴趣的:(网络,include)