将无符号字符值设置为mfc中的CString类型编辑控件 [英] Set unsigned character value to CString type edit control in mfc
本文介绍了将无符号字符值设置为mfc中的CString类型编辑控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我基于MFC对话框的应用程序中,我希望通过串口读取数据以及从中接收的任何数据,显示在编辑控件中,但不知何故它不起作用,因为我使用下面的方法:
In my MFC dialogue based application, I want to Read data through serial port and whatever data received from that,displayed into Edit control,but somehow it is not working,as I used below methodology:
/*CString msg;
msg.Format(L"%c",Buff);*/
//strcpy( (char*)Buff,ThreadStruct->m_handleDlg->m_console);//"Hello World");
while(1)
{
ReadFile(DeviceHandle,&Buff,1,&WriteBytes,NULL);
//ThreadStruct->m_handleDlg->m_OutConsole.ReplaceSel(L"\r\n\r\n\r"+msg+L"\r\n\r\n\r");
//ThreadStruct->m_handleDlg->m_OutConsole.SetWindowTextW(Buff);
//ThreadStruct->m_handleDlg->m_console.Format(L"%uc",Buff);
}
以上代码我使用了工作线程。所以使用ThreadStruct。
以上没有人正在运行,给出错误!怎么做?
for above code I used worker thread.So that used ThreadStruct.
above no one is running,gives error! How to do that?
推荐答案
你正在将一个字符(字节)读入Buff
。因此,您无法传递Buff
,其中预期NULL终止字符串参数而不附加NULL终止符。您的代码段还表明您正在使用Unicode构建。因此,从串行端口读取的字符必须转换为Unicode。使用CString
,这可以通过这种方式完成(当收到的字符是纯ASCII或来自系统的ANSI代码页时):
You are reading a single character (byte) intoBuff
. So you can't passBuff
where a NULL terminated string parameter is expected without appending the NULL terminator. Your code snippet indicates also that you are using a Unicode build. So the characters read from the serial port must be converted to Unicode. Using aCString
, this can be done this way (when the received characters are plain ASCII or from the ANSI code page of your system):
CString msg;
// Format single ASCII/ANSI char
msg.Format(L"%hc", Buff[0]);
// Format ASCII/ANSI string after appending a NULL char
Buff[1] = 0;
msg.Format(L"%hs", Buff);
h 前缀告诉format函数该参数是ASCII / ANSI字符串或字符串,并执行转换为Unicode。
The h prefix tells the format function that the parameter is an ASCII/ANSI char or string and performs the conversion to Unicode.
这篇关于将无符号字符值设置为mfc中的CString类型编辑控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文