unicode下CString转const char*的方法?

jil420 2007-10-19 01:49:55
我的vc工程在unicode环境下,请问如何将CString转为const char*呢?
多谢了!
...全文
1935 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jpjiang4648 2011-11-02
  • 打赏
  • 举报
回复
感谢akirya的方法,解决我遇到的问题
andywei1982 2008-10-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 davidMy 的回复:]
直接用函数c_Str()就可以 很方便的
[/Quote]

c_Str()是string的方法。
jil420 2007-10-19
  • 打赏
  • 举报
回复
我又试了下weiym的方法,也是可以的,只要声明成char*就行了,我刚才是const char*
另,我包含了<locale.h > ,可为什么setlocale(LC_ALL,"chs");还是不能用呢?
  • 打赏
  • 举报
回复
包含
#include<locale.h>
不然带中文的话就会错误的
jil420 2007-10-19
  • 打赏
  • 举报
回复
真是太感谢大家了,尤其akirya,我用你的方法转换成功了,但加
setlocale(LC_ALL,"chs");这句时,系统说这个函数没定义,但去掉后也能成功转换,不知为何。
好了,结贴,再次感谢!
IT界的微尘 2007-10-19
  • 打赏
  • 举报
回复
直接用函数c_Str()就可以 很方便的
  • 打赏
  • 举报
回复
你照着我的写不行么?
char* p = new char[1024];
这样分配内存声明.
arong1234 2007-10-19
  • 打赏
  • 举报
回复
楼上的问题是你需要提供一个char*数组来获得输出,然后再把那个char*传给你的函数
一般情况下T2CA应该可以吧?
jil420 2007-10-19
  • 打赏
  • 举报
回复
akirya兄提到的那个函数我不会用,我是这么写
wcstombs(chSQL,strSQL,strSQL.GetLength());
编译出错,说wcstombs' : cannot convert parameter 1 from 'const char *' to 'char *'
  • 打赏
  • 举报
回复
CString str = _T("中国abc");
setlocale(LC_ALL,"chs");
char sz[128]={0};
wcstombs(sz,str,127);
MessageBoxA(0,sz,0,0);
char*会自动 转换成const char*的
jil420 2007-10-19
  • 打赏
  • 举报
回复
谢谢weiym
我试了你的方法,好像不行,WideCharToMultiByte将cstring转成了char*,可我要转成const char*,系统提示:'WideCharToMultiByte' : cannot convert parameter 5 from 'const char *' to 'char *'
weiym 2007-10-19
  • 打赏
  • 举报
回复
CString str(_T("中国abc"));
int nLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
if(nLen > 0)
{
char* pBuf = new char[nLen];
WideCharToMultiByte(CP_ACP, 0, str, -1, pBuf, nLen, NULL, NULL);
delete []pBuf;
}
  • 打赏
  • 举报
回复
wcstombs
jil420 2007-10-19
  • 打赏
  • 举报
回复
具体情况是,在unicode下
CString strSQL;

strSQL.Format(_T("update [%s] set %s=? where %s=%d"),strTableName,PhotoField,FirstField,iFirstData);

const char* chSQL;

将strSQL转为chSQL后
sqlite3_prepare(sqlitedb.m_db, chSQL, -1, &stat, 0 );
wince上各种类型换特别是字符串是很头疼的,这个类是项目用的, 每个都测试好用,使用静态方法,直接引用后,写【::cv::就可以用】 typedef int u32; //换类,所有的换函数在这里静态 2是to的意思 //cs:cstring //s:string //i:int class cv { public: static char * s2charx(string s); //static char * cs2charx_unicode(CString s); static char * cs2charx_unicode(CString s); static CString charx2cs(char *s); cv(); virtual ~cv(); static int cstring_to_int(CString t); static CString s2cs(string text); static CString i2cs(int i); static string cs2s(CString s); static void Utf8ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static void Gb2312ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static CString string_to_cstring(string text); static int string_to_int(string str); static string cstring_to_string(CString text); static void Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static BOOL IsNumeric(string text); static void UnicodeToGb2312(char* pstrOut, u32 dwOutLen, const wchar_t* pstrIn, u32 dwInLen); static void Utf8ToGb2312(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static int cs2i(CString s); static string int_to_string(int n); static CString ChineseCapitalMoney(double Num); //static wstring s2ws( string s ); static std::wstring s2ws(const string& s); static std::string WChar2Ansi(LPCWSTR pwszSrc); static string ws2s(wstring& inputws); static const char * s2constcharx(string s); static std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen); static char * cs2charx_ansi(CString s); //static string ws2s(wstring ws); };

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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