zoukankan      html  css  js  c++  java
  • 把数字按网络顺序或主机顺序存放到字符串中++++把字符串按网络顺序转换成数字++++把字符串按主机顺序转换成数字

    /****把数字按网络顺序放到字符串中****/

    int nNumToNet(int num,char *buf,int len){
        int   ret_int;
        short ret_short;
        if(sizeof(int)!=4  || sizeof(short)!=2){
            printf("本程序不能在低于32位的机器上运行!
    ");
            exit(1);
        }
        if(len==2){
            ret_short=(short)num;
            ret_short=htons(ret_short);
            memcpy(buf,(char *)&ret_short,len);
        }else if(len==4){
            ret_int=(int)num;
            ret_int=htonl(ret_int);
            memcpy(buf,(char *)&ret_int,len);
        }else{
            printf("转换网络顺序时长度[%d]只能是2或者4!
    ",len);
            return(-1);
        }
        return(0);
    }

    /****把数字按主机顺序放到字符串中****/

    int nNumToHost(int num,char *buf,int len){
        int   ret_int;
        short ret_short;
        if(sizeof(int)!=4  || sizeof(short)!=2){
            printf("本程序不能在低于32位的机器上运行!
    ");
            exit(1);
        }
        if(len==2){
            ret_short=(short)num;
            memcpy(buf,(char *)&ret_short,len);
        }else if(len==4){
            ret_int=(int)num;
            memcpy(buf,(char *)&ret_int,len);
        }else{
            printf("转换网络顺序时长度[%d]只能是2或者4!
    ",len);
            return(-1);
        }
        return(0);
    }

    /****把字符串按网络顺序转换成数字****/

    int nNetToNum(char *buf,int len){
        int   ret_int;
        short ret_short;
        if(sizeof(int)!=4  || sizeof(short)!=2){
            printf("本程序不能在低于32位的机器上运行!
    ");
            exit(1);
        }
        if(len==2){
            memcpy((char *)&ret_short,buf,len);
            ret_short=ntohs(ret_short);
            return((int)ret_short);
        }else if(len==4){
            memcpy((char *)&ret_int,buf,len);
            ret_int=ntohl(ret_int);
            return((int)ret_int);
        }else{
            printf("转换网络顺序时长度[%d]只能是2或者4!
    ",len);
            return(-1);
        }
        return(0);
    }

    /****把字符串按主机顺序转换成数字****/

    int nHostToNum(char *buf,int len){
        int   ret_int;
        short ret_short;
        if(sizeof(int)!=4  || sizeof(short)!=2){
            printf("本程序不能在低于32位的机器上运行!
    ");
            exit(1);
        }
        if(len==2){
            memcpy((char *)&ret_short,buf,len);
            return((int)ret_short);
        }else if(len==4){
            memcpy((char *)&ret_int,buf,len);
            return((int)ret_int);
        }else{
            printf("转换网络顺序时长度[%d]只能是2或者4!
    ",len);
            return(-1);
        }
        return(0);
    }
  • 相关阅读:
    Python_代码练习_写一个判断是否为小数的函数
    Python学习杂记_11_函数(一)
    Ubuntu Server 16.04设置WiFi
    ubuntu 16.04 php 安装curl方法
    Ubuntu上搭建Git服务器
    Ubuntu 16.04 安装 Apache, MySQL, PHP7
    iOS 创建framework & bundle 主要配置
    ios 改变push方向,可以把present改为push方式
    ubuntu环境下使用apt-get配置apache+php+mysql
    [Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法
  • 原文地址:https://www.cnblogs.com/sherlockhomles/p/3213840.html
Copyright © 2011-2022 走看看