zoukankan      html  css  js  c++  java
  • mfc报文相关算法

    1、传入_T("AAAABBBBCC"),返回_T("AA AA BB BB CC")

    CString FormatPacket(CString packet_str)
    {
      packet_str.Replace(_T(" "),_T(""));
      CString packet_backspace_str;//带空格的包
      CString temp_str;//临时存一个十六进制
      int j = 0;
      /*这里加上空格*/
      for (int i = 0;i<packet_str.GetLength();i++)
        {
        temp_str = packet_str.GetAt(i++);
        temp_str += packet_str.GetAt(i);
        temp_str += _T(" ");
        packet_backspace_str += temp_str;
      }
      return packet_backspace_str;
    }

    2、传入_T("AAAABBBBCC"),返回_T("AAAABBBBCCXX"),XX是校验和

    CString PutCheckByte(CString packet_str,BOOL RESULT4TAX)//
    {
      CString temp_str;
      int packet_lenth_int = packet_str.GetLength();
      unsigned short sum =0;
      for(int i=0;i<packet_lenth_int;i++)
        {
        temp_str = packet_str.GetAt(i++);
        temp_str += packet_str.GetAt(i);
        sum += wcstol(temp_str,NULL,16);
        }
      sum = sum % 256;
      CString checksum_result;
      checksum_result.Format(_T("%02x"),sum);
      packet_str += checksum_result;
      return packet_str;//返回的是整个报文

    }

    3、传入_T("你好123"),返回7,这个字符串实际所占长度

    int GetByteLenth(CString unicode_str)
    {
      int lenth_int;//字符数,5
      lenth_int = unicode_str.GetLength();
      int result_int = 0;
      for (int i=0;i<lenth_int;i++)
        {
        if((unicode_str.GetAt(i) >= 48 && unicode_str.GetAt(i) <= 57) ||(unicode_str.GetAt(i) >= 65 && unicode_str.GetAt(i) <= 90)||(unicode_str.GetAt(i)     >= 97 && unicode_str.GetAt(i) <= 122))
          result_int += 1;
        else
          result_int += 2;
        }
      return result_int;
    }

    4、传入字节数组,BYTE array[10],返回带空格CString

    CString UChar2CString(BYTE* data_byte,int count)//count代表data_byte的字节数
    {
      CString temp_str;
      CString result_str;
      temp_str = _T("");
      result_str = _T("");
      for (int i=0;i<count;i++)
        {
        temp_str.Format(_T("%02x"),data_byte[i]);
        result_str += temp_str;
        result_str += _T(" ");
        }
      result_str = result_str.MakeUpper();
      return result_str;
    }

  • 相关阅读:
    HDFS snapshot操作实战
    不是技术牛人,如何拿到国内IT巨头的Offer(转载)
    HBase的RowKey设计原则
    hbase shell 基本命令总结
    13_Python数据类型字符串加强_Python编程之路
    监督学习与无监督学习的区别_机器学习
    12_Python的(匿名函数)Lambda表达式_Python编程之路
    Python数据挖掘_Python2模块Spynner的安装(安装失败)
    06_Linux目录文件操作命令3查找命令_我的Linux之路
    python数据挖掘_Json结构分析
  • 原文地址:https://www.cnblogs.com/judes/p/6406303.html
Copyright © 2011-2022 走看看