天涯爱程序 2016-06-22 01:46 采纳率: 0%
浏览 1750
已结题

CByteArray 和 CString 如何使用,麻烦大侠们看看我这个代码

#include
#include
#include
using namespace std;
#include
#include
#define DEV_CHECK_OUT 0
int Str2Byte(CString strProtocol,CByteArray bytArProtocol)
{
int nSteLen = 0;
nstrLen = strProtocol.GetLength();

int nBlockLen = 2;

bytArProtocol.SetSize(nstrLen/2);   

for (int i = 0; i < nstrLen; i += nBlockLen)
{
    int nData = 0;
    _stscanf(strProtocol.Mid(i, 2), _T("%x"), &nData);
    bytArProtocol.SetAt(i/2, unsigned char(nData));             
}

return 0;

}
int Byte2Str(CByteArray bytArProtocol,CString strProtocol)
{
strProtocol = _T("");
CString strTemp = _T("");
for (int i = 0; i < bytArProtocol.GetSize(); i ++)
{
strTemp.Format(_T("%.2X"), bytArProtocol.GetAt(i));

strProtocol += strTemp;
}

return 0;

}

BOOL mfAddSumCRC(CByteArray bytArProtocol)
{
int nStartLen = 5;
int nCheckDir = 0;
int nCrcLen = bytArProtocol.GetSize() - 2;

int i = 0, j = 0;
int MSBInfo;
WORD wCrcData = 0x0000;

/////////////////////////////////////////
if (nCheckDir == DEV_CHECK_OUT)
{
    /////////////////STN//////////////////////////
    wCrcData = wCrcData ^ (bytArProtocol.GetAt(2));
    if (wCrcData < 0) wCrcData = wCrcData + 65536; 
    for (j= 0; j < 8; j ++)
    {
        MSBInfo = wCrcData & 0x0001;
        wCrcData = wCrcData >> 1;
        if (MSBInfo != 0 )
        {
            wCrcData = wCrcData ^ 0xa001;
        }
    }
    ////////////////STX///////////////////////////
    wCrcData = wCrcData ^ STX;
    if (wCrcData < 0) wCrcData = wCrcData + 65536; 
    for (j= 0; j < 8; j ++)
    {
        MSBInfo = wCrcData & 0x0001;
        wCrcData = wCrcData  >> 1;
        if (MSBInfo != 0 )
        {
            wCrcData = wCrcData ^ 0xa001;
        }
    }
}
/////////////////////////////////////////

for(i = nStartLen; i < nCrcLen; i++)
{
    wCrcData = wCrcData ^ (bytArProtocol.GetAt(i));
    if (wCrcData < 0) wCrcData = wCrcData + 65536; 
    for (j = 0; j < 8; j ++)
    {
        MSBInfo = wCrcData & 0x0001;
        wCrcData = wCrcData >> 1;
        if (MSBInfo != 0 )
        {
            wCrcData = wCrcData ^ 0xa001;
        }
    }
}
///////////////////ETX///////////////////
wCrcData = wCrcData ^ ETX;
if (wCrcData < 0) wCrcData = wCrcData + 65536; 
for (j = 0; j < 8; j ++)
{
    MSBInfo = wCrcData & 0x0001;
    wCrcData = wCrcData >> 1;
    if (MSBInfo != 0 )
    {
        wCrcData = wCrcData ^ 0xa001;
    }
}

switch (nCheckDir)
{
case 0:
    // 发数据时校验
    {   
        bytArProtocol.Add((wCrcData&0xff));
        bytArProtocol.Add((wCrcData>>8&0xff));

    }break;
case 1:
    // 收数据时校验
    {
        nCrcLen = bytArProtocol.GetSize();

        if ((wCrcData&0xff) != bytArProtocol.GetAt(nCrcLen - 2) 
            || (wCrcData>>8&0xff) != bytArProtocol.GetAt(nCrcLen - 1))  
        {               
            return FALSE;
        }
    }break;
default:
    {
    }break;
}

return TRUE;

}
int main()
{
CByteArray bytArProtocol;
string strProtocol;
cin >> strProtocol;
Str2Byte(strProtocol,bytArProtocol);
mfAddSumCRC(bytArProtocol);
Byte2Str(bytArProtocol,strProtocol);
cout << strPrototol << endl;

}

这是在VS2008生成解决方案的错误信息:
1>------ 已启动生成: 项目: ABCRC, 配置: Debug Win32 ------
1>正在编译...
1>BullseyeCoverage Compile C++ 7.13.48 Windows License 1001
1>Copyright (c) Bullseye Testing Technology 1990-2010
1>ABCRC.cpp
1> WINVER not defined. Defaulting to 0x0600 (Windows Vista)
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(11) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(14) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(16) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(65) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(94) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(138) : error C2664:
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(140) : error C2664:
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(141) : error C2065:
1>Exception: error status 2 from d:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin/cl.exe
1>生成日志保存在“file://e:\练习文件\ABCRC\ABCRC\Debug\BuildLog.htm”
1>ABCRC - 8 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

  • 写回答

1条回答 默认 最新

  • Eleven 2016-06-22 07:13
    关注

    请参考MSDN文档对CByteArray/CString的用法~

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组