CString怎么转换成 const char *

苏克贝塔03 2010-07-29 09:20:02
我定义是CString strSend;
编译时说:cannot convert parameter 2 from 'const char [1]' to 'LPCTSTR'
请问怎么转啊???
...全文
421 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞翔的薄荷 2010-07-29
  • 打赏
  • 举报
回复
不好意思 改下
::MessageBoxA(0,s,0,0);
飞翔的薄荷 2010-07-29
  • 打赏
  • 举报
回复
//Unicode下

setlocale(LC_ALL,"chs");
CString str("你好啊");
char *s = new char[str.GetLenght()*2+1];
wcstombs(s,str,str.GetLenght()*2+1);
::MessageBox(0,s,0,0);
Eleven 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 jichao1010 的回复:]
应该是(LPSTR)(LPCTSTR)strSend;
[/Quote]
你这样在Unicode下没有问题吗?
路人乙2019 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jichao1010 的回复:]

引用 1 楼 stonewater 的回复:
看看你的函数,你的工程应该是UNICODE的,所以'LPCTSTR'是const wchar_t *

我的是vs2005好像是UNICODE,但是怎么改呢?
我非常糊涂,ascan,unicode到底有啥区别啊?
[/Quote]
vs2005默认情况下支持unicode.
ASCII: 一种使用7个或8个二进制位进行编码的方案,最多可以给256个字符(包括字母、数字、标点符号、控制字符及其他符号)分配(或指定)数值.
UNICODE:给每个字符提供了一个唯一的数字,不论是什么平台,不论是什么程序,不论是什么语言。使用16位的编码空间。也就是每个字符占用2个字节。
char表示的是ASCii字符,wchar_t表示宽字符即UNICODE.
苏克贝塔03 2010-07-29
  • 打赏
  • 举报
回复
应该是(LPSTR)(LPCTSTR)strSend;
苏克贝塔03 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 nbabest23 的回复:]
传(LPCTSTR)strSend
[/Quote]
试过了,好了。
swlilike 2010-07-29
  • 打赏
  • 举报
回复
cstring.getbuffer();

用完记得释放!
releasedbuffer
nbabest23 2010-07-29
  • 打赏
  • 举报
回复
传(LPCTSTR)strSend
苏克贝塔03 2010-07-29
  • 打赏
  • 举报
回复
int sendto(
SOCKET s,
const char FAR* buf, int len,
int flags,
const struct sockaddr FAR* to,
int tolen
);
就是那个参数,CString 类型的怎么传给它啊?
苏克贝塔03 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stonewater 的回复:]
看看你的函数,你的工程应该是UNICODE的,所以'LPCTSTR'是const wchar_t *
[/Quote]
我的是vs2005好像是UNICODE,但是怎么改呢?
我非常糊涂,ascan,unicode到底有啥区别啊?
xueer8835 2010-07-29
  • 打赏
  • 举报
回复
CString to LPCSTR
将CString转换成LPCSTR,需要获得CString的长度,例如:
CString cStr = _T( "Hello,world! ");
int nLen = cStr.GetLength();
LPCSTR lpszBuf = cStr.GetBuffer(nLen);
梵海木 2010-07-29
  • 打赏
  • 举报
回复
我的工程是UNICODE

WCHAR* szAttribute;
szAttribute = new WCHAR[size];

size_t i = 0;
wcscpy(szAttribute, L"DSN=");
梵海木 2010-07-29
  • 打赏
  • 举报
回复
转自http://hi.baidu.com/tmpwh2002/blog/item/57a06000c9153614738b6553.html


1.传给未分配内存的const char* (LPCTSTR)指针.

CString cstr(asdd);

const char* ch = (LPCTSTR)cstr;

ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.

//2.传给未分配内存的指针.

CString cstr = "ASDDSD";

char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);

cstr.ReleaseBuffer();

//修改ch指向的值等于修改cstr里面的值.

//PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.

//3.第二种用法。把CString 值赋给已分配内存的char *。

CString cstr1 = "ASDDSD";

int strLength = cstr1.GetLength() + 1;

char *pValue = new char[strLength];

strncpy(pValue, cstr1, strLength);



//4.第三种用法.把CString 值赋给已分配内存char[]数组.

CString cstr2 = "ASDDSD";

int strLength1 = cstr1.GetLength() + 1;

char chArray[100];

memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.

strncpy(chArray, cstr1, strLength1);
icefairy 2010-07-29
  • 打赏
  • 举报
回复
CString str =" sdfsdf";
char szTest[100];
memcpy(szTest ,_T(str) ,str,getlength);
雪影 2010-07-29
  • 打赏
  • 举报
回复
直接强制转换即可
stonewater 2010-07-29
  • 打赏
  • 举报
回复
看看你的函数,你的工程应该是UNICODE的,所以'LPCTSTR'是const wchar_t *
苏克贝塔03 2010-07-29
  • 打赏
  • 举报
回复
不懂,弄不明白了,结贴去。。
luodx1021 2010-07-29
  • 打赏
  • 举报
回复
(unsigned char *)strSend.operator LPCTSTR()
qianlicao1208 2010-07-29
  • 打赏
  • 举报
回复
设置下属性
黑泡泡选手 2010-07-29
  • 打赏
  • 举报
回复
GetBuffer();
rsa算法设计程序 #if !defined(AFX_RSAA_H__081D9EE0_1245_11D5_80AC_0000E8810675__INCLUDED_) #define AFX_RSAA_H__081D9EE0_1245_11D5_80AC_0000E8810675__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // RsaA.h : header file // #include #include #include<string.h> #include const DATALENGTH=350; //大数类型的长度 const MLENGTH=10; //质数的长度 const TESTNUM=30; //测试质数时的比较的次数 const SKLENGTH=4; //私钥的长度 typedef signed char byteint[DATALENGTH]; //定义大数类型 typedef signed char mtype[MLENGTH]; //定义质数的大数类型 //extern "C" __declspec(dllexport) void RsaAGenKeys(CString& pk,CString& sk,CString& R); //提供给服务器使用的秘钥产生函数 //extern "C" __declspec(dllexport) int RsaAEncrypt(CString& source,const char* key,const char* R,CStringArray& result);//加密 //extern "C" __declspec(dllexport) CString RsaADecrypt(CStringArray& source,const char* sk,const char* R);//解密 ///////////////////////////////////////////////////////////////////////////// // CRsaA command target //实现RSA算法的类 class CRsaA : public CCmdTarget { DECLARE_DYNCREATE(CRsaA) CRsaA(); // protected constructor used by dynamic creation // 成员函数 private: void InitInt(void); //基本数据常量的初始化 int IntValid(byteint validtemp); //返回大数validtemp的非零位的个数 int IntCmp(byteint A,byteint B); //比较大数A和B是否相等 //自定义类型的基本运算 void Plus(byteint A,byteint B,byteint C); //C=A+B void Substract(byteint SA,byteint SB,byteint SC); //SC=SA-SB void Multiply(byteint A,byteint B,byteint C); //C=A*B void SetMode(byteint A,byteint B,byteint C,byteint D);//C=A%B int PowerMode(byteint A,byteint C,byteint D,signed char flag[400]);//computing A^B mod C-->D void IntRandom(byteint RandomA,int num); //随机产生一个大数 void LoadInt(byteint A,mtype B); //将质数类型转换为大数类型 void TransBi(byteint B,signed char flag[400]); //将大数B转换为二进制形式 void Mdata(); //产生用于生成质数中进行比较的数 int Prime(byteint Prm); //产生一个长度为MLENGTH的质数 int ComputingPK(byteint Rvalue,byteint SK,byteint PK); //计算公钥PK void ComputingR(byteint p,byteint q,byteint R); //计算模值R void ComputingRvalue(byteint p,byteint q,byteint Rvalue); //计算$(r) void IntCpy(byteint A1,byteint B1); //将大数B1的值拷贝到大数A1中 void SetZero(byteint A); //将大数A清零 CString PrtInt(byteint A); //将一个大数类型转换为一个CString类型 int Getinput(byteint result,CString input); //将字符串转换为对应的大数形式 int Getinput1(byteint result,unsigned long input); //将长整形数转换为对应的大数形式 void RsaDo(byteint source,byteint R,byteint key,byteint desti); //实现加解密 unsigned long Os2ip(unsigned char* pstr); CString Ip2os(CString str); public: void GenKeys(CString& pk,CString& sk,CString& R); //提供给服务器使用的秘钥产生函数 int RsaEncrypt(CString& source,const char* key,const char* R,CStringArray& result);//加密 CString RsaDecrypt(CStringArray& source,const char* sk,const char* R);//解密 void GenKeysTable(); //生成秘钥对文件 void LoadKeysFromFile(CString& r,CString& sk,CString& pk); //成员变量 private: byteint ONEVALUE; byteint ZEROVALUE; byteint TWOVALUE; byteint EIGHTVALUE; //O,1,2,8 constant mtype Model[TESTNUM]; //TESTNUM big number to be compared mtype mZEROVALUE,tempModel; //0 constant signed char flag[400]; // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CRsaA) //}}AFX_VIRTUAL // Implementation //protected: virtual ~CRsaA(); // Generated message map functions //{{AFX_MSG(CRsaA) // afx_msg LONG OnComputing(WPARAM wParam, LPARAM lParam); // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations im
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); };

16,472

社区成员

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

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

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