wchar_t”转换到“char”

ITcaichong123 2012-05-05 12:30:48
 CString str;
 GetDlgItem(IDC_EDIT_DeviceName)->GetWindowTextW(str);
  for(int i = 0;i<str.GetLength();i++)
{
m_listitem.m_sername[i] = str.GetAt(i);//不报错,但是转换后的结果不一样

}
 其中m_listitem是结构typedef struct
{
unsigned char m_mac[6];
char m_sername[40];
char m_url[20];
char m_submask[20];
char m_getway[20];
char m_multiip[20];
char m_dns[20];
WORD m_localport;
WORD m_webport;
WORD m_multiport;
}LIST_ITEM;的变量
运行有警告 >SetParameter.cpp(80): warning C4244: “=”: 从“wchar_t”转换到“char”,可能丢失数据
求高手指教,谢谢!!!!
...全文
1027 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
last__smile 2013-08-12
  • 打赏
  • 举报
回复
我也遇到这种情况
ITcaichong123 2012-05-14
  • 打赏
  • 举报
回复
这样也不行哦?哎郁闷到底怎么回事啊?
zgl7903 2012-05-07
  • 打赏
  • 举报
回复
CString str;
GetDlgItemText(IDC_EDIT_DeviceName, str);
strcpy_s(m_listitem.m_sernme, CStringA(str));
cy2015yc 2012-05-07
  • 打赏
  • 举报
回复

// 功能: WideChar字符串转换为ANSI
// 参数:[in]szWidechar - 被转换的WideChar字符串
// 返回值: char* 转换后的ANSI字符串指针,调用者需要delete
char* CUtility::WideCharToANSI( const wchar_t* szWidechar )
{
ASSERT(szWidechar != NULL);

int widecharLen = ::WideCharToMultiByte(CP_ACP, 0, szWidechar, -1, NULL, 0, NULL, NULL);
char* pAnsi = new char[widecharLen+1];
memset(pAnsi, 0x00, widecharLen+1);
::WideCharToMultiByte(CP_ACP, 0, szWidechar, -1, pAnsi, widecharLen, NULL, NULL);

return pAnsi;
}
昨夜无风 2012-05-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
CString str;
GetDlgItem(IDC_EDIT_DeviceName)->GetWindowTextW(str);
WideCharToMultiByte(CP_OEMCP, 0, str, -1, m_listitem.m_sername, str.GetLength(), NULL, NULL);
我改成这样后没有告警,但是结果不一样了,我改的是SE-WA03test……
[/Quote]

int nLen = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);
m_listitem.m_sername[nLen]='\0';
ITcaichong123 2012-05-07
  • 打赏
  • 举报
回复
CString str;
GetDlgItem(IDC_EDIT_DeviceName)->GetWindowTextW(str);
WideCharToMultiByte(CP_OEMCP, 0, str, -1, m_listitem.m_sername, str.GetLength(), NULL, NULL);
我改成这样后没有告警,但是结果不一样了,我改的是SE-WA03test但是
输出结果: listitem->m_sername 0x00c2fdbe "SE-WA03test?92.168.1.103"
后面不知怎么就多IP,中间还有那么多空格?求指教
wqvbjhc 2012-05-05
  • 打赏
  • 举报
回复
把LIST_ITEM改为
typedef struct
{
unsigned TCHAR m_mac[6];
TCHAR m_sername[40];
TCHAR m_url[20];
TCHAR m_submask[20];
TCHAR m_getway[20];
TCHAR m_multiip[20];
TCHAR m_dns[20];
WORD m_localport;
WORD m_webport;
WORD m_multiport;
}LIST_ITEM;
poorb 2012-05-05
  • 打赏
  • 举报
回复
图方便可以使用 W2A()直接转换, 前提是加入 atlbase.h 头文件。
向立天 2012-05-05
  • 打赏
  • 举报
回复
要做字符编码转换
用WideCharToMultiByte
从宽字节转到多字节
Eleven 2012-05-05
  • 打赏
  • 举报
回复
WideCharToMultiByte(CP_ACP, ...);
dahaiI0 2012-05-05
  • 打赏
  • 举报
回复

UNICODE下CString转换成char*的方法

方法1: CString str = _T("测试用例");
int nLength = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);
char *pBuffer = new char[nLength + 1];
WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),pBuffer,nLength,NULL,NULL);
pBuffer[nLength] = 0;

方法2:

CString s = _T("测试用例");
USES_CONVERSION;
char* lpsz = T2A(s.GetBuffer(s.GetLength()));
s.ReleaseBuffer(s.GetLength());


CString的GetAt,返回的是一个WCHAR,赋值给char,会有丢失

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧