读书人

CString转化为unsigned char,该如何解

发布时间: 2012-02-15 12:09:44 作者: rapoo

CString转化为unsigned char
typedef unsigned int uint;
typedef unsigned char uchar; //指向无符号字符的指针
uint cal_crc(uchar *byte, uchar nbyte)
{
uint itemp=0xFFFF;
uchar i;
while(nbyte--)
{
itemp ^= *byte++ << 8;
for (i=0; i<8; i++)
{
if (itemp & 0x8000)
{
itemp <<= 1;
itemp ^= 0x1021; //定义的生成多项式0x1021
}
else
itemp <<= 1;
}
}
return itemp;
}

DWORD length;
CString strTemp;
strTemp="E0800"; //读取通道0的温度
uint str1=cal_crc((LPTSTR)(LPCTSTR)strTemp,4); //就是要在指令后加4字符的CRC校验,类型不对?
strcpy(strTemp,str1);//?就是这两句错
OVERLAPPED m_osWrite;
memset(&m_osWrite, 0, sizeof(OVERLAPPED));
m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
WriteFile(hCom,strTemp,8,&length,&m_osWrite);

[解决办法]
你想多了:
CString m_SRC="中文abc";///-///源字符串
int m_int_Length=m_SRC.GetLength();
char *buff=new char[m_int_Length+1];
unsigned char *ucbuff=new unsigned char[m_int_Length];
sprtinf(buff,m_SRC,m_int_Length);
for(int i=0;i<m_int_Length;i++)
{
ucbuff[i]=(BYTE)buff[i];
}
delete [] buff;
///-///ucbuff应该就是你要的unsigned char数组,用过之后,也delete。
delete [] ucbuff;





[解决办法]

C/C++ code
WORD MakeCRC(UCHAR *puchMsg, WORD usDataLen);UCHAR SendBuf[1024];SendBuf[0] = (UCHAR)Station ;SendBuf[1] = 3 ;SendBuf[2] = HIBYTE(addr) ;SendBuf[3] = LOBYTE(addr) ;SendBuf[4] = HIBYTE(nCount) ;SendBuf[5] = LOBYTE(nCount) ;nCRC = MakeCRC(SendBuf,6) ;SendBuf[6] = HIBYTE(nCRC) ;SendBuf[7] = LOBYTE(nCRC) ; 

读书人网 >VC/MFC

热点推荐