项目中遇到了串口通讯的问题。想看一下从串口中读出的数据,读出的数据我放在了一个TCHAR指针变量里,发现在变成十六进制字符串输出时,本来的0xf2,变完之后会成为ffffff2(几个f忘了),换了几个变换方式,都是这样的问题。后来才想到,是符号的问题。需要先把变量转换成无符号的整数,再转换为十六进制。我想到的是将TCHAR强制转换为BYTE,试了下可行。
- void CMagneticEq::Output(const BYTE *data, int len)
- {
- static int num=0;
- CString temp;
- temp.Format(_T("begin%d:"),num++);
- CString ttttt;
- for (int i=0;i<len;i++)
- {
- //itoa(data[i],test,16);
- ttttt.Format("%x",data[i]);
- temp+=ttttt;
- temp+=" ";
- }
- temp+="\r\n";
- CString ScriptError=_T("D:\\Output.txt");
- CFile m_ScriptError;
- BOOL st = m_ScriptError.Open(ScriptError,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
- if (st == FALSE)
- {
- AfxMessageBox("打开文件D:\\Output.txt失败!");
- //return FALSE;
- }
- else
- {
- m_ScriptError.SeekToEnd();
- m_ScriptError.Write(temp,temp.GetLength());
- m_ScriptError.Flush();
- m_ScriptError.Close();
- }
- }