您当前的位置:delphi教程 > Delphi笔记 >  htonl 、htns、ntohl,ntohs相关的几个网络数值函数的实现 (附带一个工具库)

htonl 、htns、ntohl,ntohs相关的几个网络数值函数的实现 (附带一个工具库)

不才近期在做一个短短消息,相关的软件,由于网关提供商,只提供了网络协议没有提供现有的API。所以,我才想到自己写一份,因为网络协议上要求必须转换成,网络顺序字节整数,所以
我才想自己写一份。本来系统准备使用Delphi开发,有跨平台的要求和考虑到代码的实际可用性,所以才使用C++开发。java本来也是一个选择,考虑到可能要给用户提交C++静态库,所以
(Delphi也是可以跨平台的,但是好像Delphi的跨平台功能使用的不是很广泛。请各位DFW不要生气。生气会犯嗔戒的:P)

/*
  用于准换字节和整数,用于替换ms 的nltoh,hlton,nstoh,hston
  函数增加代码的可用性

*/
#if !defined _UTILITY_H
#define _UTILITY_H
#pragma once

 
 class Utility
{

 public: 
   inline static short charsToShort( char achar[])//字节数组到字
   {
	    int j;
		int length=sizeof(achar);
        if(length <= 2)
            j = length;
        else
            j = 2;
        int i = 0;
        for(int k = 0; k < j; k++)
            i = (i << 8) + (achar[k] & 0xff);

        return i;
   }
   inline  static int charsToInt( char achar[])//字节数组到整数
    {
        int j;
		int length=sizeof(achar);
        if(length <= 4)
            j = length;
        else
            j = 4;
        int i = 0;
        for(int k = 0; k < j; k++)
            i = (i << 8) + (achar[k] & 0xff);

        return i;
    }

    inline   static  char* decodeMsgID( char achar[])//消息ID解码
    {
         char* achar1=new  char(20);
        int i;
	     int length=sizeof(achar);
        if(length < 10)
            i = length;
        else
            i = 10;
        for(int j = 0; j < i; j++)
        {
            achar1[j * 2] = (char)((achar[j] >> 4 & 0xf) + 48);
            achar1[j * 2 + 1] = (char)((achar[j] & 0xf) + 48);
        }

        return achar1;
    }

    inline   static  char* intTochars(int i, char* achar0)//整数到字节转换
    {
       
		 Zerochars(achar0,4);
        achar0[3] = ( char)i;
        i >>= 8;
        achar0[2] = i;
        i >>= 8;
        achar0[1] = i;
        i >>= 8;
        achar0[0] = i;
        return achar0;
    }
	inline static  char* shortTochars(short i,  char* achar0 )//字到字节转换
	{
		
		Zerochars(achar0,2);
        achar0[1] = ( char)i;
        i >>= 8;
        achar0[0] = i;
        
        return achar0;
	}
	inline static void exchange(char* chars,int len)
	{
		for(int i=0;i<len/2;i++)
		{   char temp=chars[i];
			chars[i]=chars[len-i-1];
			chars[len-i-1]=temp;
		}
	}
	inline static void Zerochars(char* chars,int len)
	{
		for(int i=0;i<len;i++)
		{
			chars[i]=0;
		}
	}
inline static  int  HToNL(const int value)//等同 ms htonl
{ char theb[4];
  Utility::Zerochars(theb,4);
  int nl=0;
  memcpy(theb,&value,4);
  Utility::exchange(theb,4);
  memcpy(&nl,theb,4);
  
  return nl;


}
inline static int   NToHL(int nl)//等同 ms ntohl
{
  char theb[4];
  Utility::Zerochars(theb,4);
  int hl=0;
  memcpy(theb,&nl,4);
  Utility::exchange(theb,4);
  memcpy(&hl,theb,4);
  
  return hl;

}
 
 };
 #endif
主机字节序转换到网络字节序列的原理:
网络字节序列采用低字节在高位的排列方式,
而X86主机字节序列采用地字节在低位的方式
其实只要交换一下就可以实现网络字节序列和主机字节序列的转换,
所以对于上面的函数,htonl和nthl是一样的。但是微软却封装成两个不同
的函数,所以我们只要对网络数值采用htonl就可以转换成主机字节。


查看回复
关于我们 | 网站地图 | 广告刊登 | 友情链接
Copyright ©2008 - 2010  Delphi2007.net  ,All Rights Reserved  粤ICP备09038647号
业务联系:allceoad#gmail.com QQ:20008835 最佳分辨率 1024×768