C++ string

C++ string best practices => LPTSTR, PSTR, CString, _T, TEXT, Win32 API, Win16. string, wstring.

strings

http://msdn.microsoft.com/en-us/library/ms174288.aspx

LPTSTR

http://baike.baidu.com/view/3186101.htm

Using CString       very  useful

http://msdn.microsoft.com/en-us/library/ms174288.aspx

CryptStringToBinary <=> CryptBinaryToString

// CString_example_1.cpp : Defines the entry point for the console application.
// #include "stdafx.h" #include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <string>
#include <wincrypt.h>
#include <iostream> // std::cout, std::end #pragma comment(lib, "crypt32.lib") int _tmain(int argc, _TCHAR* argv[])
{ // Chinese characters for "zhongwen" ("Chinese language").
const BYTE kChineseSampleText[] = { -, -, -, -, -, -, };
//-28 => 1110,0100 => 228
//-72 => 1011,1000 => 184
//-83 => 1010,1101 => 173
//-26 => 1110,0110 => 230
//-106 => 1001,0110 => 150
//-121 => 1000,0111 => 135 //const char kChineseSampleText[] = "\xe4\xb8\xad\xe6\x96\x87";
//const char kChineseSampleText[] = "\e4\b8\ad\e6\96\87"; DWORD strLen = ;
CryptBinaryToString(kChineseSampleText, ,
CRYPT_STRING_HEXRAW,
NULL,
&strLen
); LPTSTR string1 = new TCHAR[strLen + ]; CryptBinaryToString(kChineseSampleText, ,
CRYPT_STRING_HEXRAW,
string1,
&strLen
); string1[strLen] = '\0';
LPCTSTR stringC = string1; //string strii = wprintf(string1, "%s"); //string to bytes DWORD strCLen = ; CryptStringToBinary(
stringC,
strLen,
CRYPT_STRING_HEXRAW,
NULL,
&strCLen,
, ); BYTE* cwStr = new BYTE[strCLen + ]; CryptStringToBinary(
stringC,
strLen,
CRYPT_STRING_HEXRAW,
cwStr,
&strCLen,
, ); for (int i = ; i < ; i++)
{
BYTE x = cwStr[i];
x++;
} return ;
}

Wstring -> bytes -> base64 -> bytes -> Wstring Example

// CString_example_1.cpp : Defines the entry point for the console application.
// #include "stdafx.h" #include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <string>
#include <wincrypt.h>
#include <iostream> // std::cout, std::end #pragma comment(lib, "crypt32.lib") void WstringToBytes(LPWSTR wszString, char* szAnsi, DWORD* bytesSize)
{
*bytesSize = WideCharToMultiByte(CP_UTF8, NULL, wszString, -, NULL, , NULL, FALSE); WideCharToMultiByte(CP_UTF8, NULL, wszString, -, szAnsi, *bytesSize, NULL, FALSE);
} void BytesToWstring(char* bytes, LPWSTR wszString)
{
DWORD dwNum = MultiByteToWideChar(CP_UTF8, , bytes, -, NULL, ); MultiByteToWideChar(CP_UTF8, , bytes, -, wszString, dwNum); //×îºó¼ÓÉÏ'\0'
wszString[dwNum] = '\0';
} int _tmain(int argc, _TCHAR* argv[])
{ // Chinese characters for "zhongwen" ("Chinese language").
//const BYTE kChineseSampleText[] = { -28, -72, -83, -26, -106, -121, 0 };
//-28 => 1110,0100 => 228
//-72 => 1011,1000 => 184
//-83 => 1010,1101 => 173
//-26 => 1110,0110 => 230
//-106 => 1001,0110 => 150
//-121 => 1000,0111 => 135 //const char kChineseSampleText[] = "\xe4\xb8\xad\xe6\x96\x87";
//const char kChineseSampleText[] = "\e4\b8\ad\e6\96\87"; LPWSTR plainText = L"abcd1234";
DWORD bytesLen = ;
char* buffer = new char[]; WstringToBytes(plainText, buffer, &bytesLen); BYTE* kChineseSampleText = new BYTE[bytesLen + ];
memcpy(kChineseSampleText, buffer, bytesLen); DWORD strLen = ;
CryptBinaryToString(kChineseSampleText, bytesLen,
CRYPT_STRING_BASE64, //CRYPT_STRING_HEXRAW, CRYPT_STRING_BASE64
NULL,
&strLen
); LPTSTR string1 = new TCHAR[strLen + ]; CryptBinaryToString(kChineseSampleText, bytesLen,
CRYPT_STRING_BASE64, //CRYPT_STRING_HEXRAW, CRYPT_STRING_BASE64
string1,
&strLen
); string1[strLen] = '\0';
LPCTSTR stringC = string1; //string strii = wprintf(string1, "%s"); //string to bytes DWORD strCLen = ; CryptStringToBinary(
stringC,
strLen,
CRYPT_STRING_BASE64,
NULL,
&strCLen,
, ); BYTE* cwStr = new BYTE[strCLen + ]; CryptStringToBinary(
stringC,
strLen,
CRYPT_STRING_BASE64, //CRYPT_STRING_HEXRAW, CRYPT_STRING_BASE64
cwStr,
&strCLen,
, ); cwStr[strCLen] = '\0';
/*for (int i = 0; i < 6; i++)
{
BYTE x = cwStr[i];
x++;
}*/ LPWSTR result = (LPWSTR)malloc();
BytesToWstring((char*)cwStr, result); return ;
}

Example

UTF16 to UTF8 to UTF16 simple CString based conversion

http://www.codeproject.com/Articles/26134/UTF-to-UTF-to-UTF-simple-CString-based-conver

wstring => char*

char* = > wstring

int _tmain(int argc, _TCHAR* argv[])
{
/*char sText[20] = { "多字节字符串!OK!" }; //bug; it is bytes DWORD dwNum = MultiByteToWideChar(CP_UTF8, 0, sText, -1, NULL, 0); wchar_t* pwText;
pwText = new wchar_t[dwNum]; MultiByteToWideChar(CP_UTF8, 0, sText, -1, pwText, dwNum);*/ wchar_t wText[] = { L"宽字符转换实例!OK!" }; DWORD dwNum2 = WideCharToMultiByte(CP_UTF8, NULL, wText, -, NULL, , NULL, FALSE);
char* psText;
psText = new char[dwNum2]; WideCharToMultiByte(CP_UTF8, NULL, wText, -, psText, dwNum2, NULL, FALSE); DWORD dwNum = MultiByteToWideChar(CP_UTF8, , psText, -, NULL, ); wchar_t* pwText;
pwText = new wchar_t[dwNum]; MultiByteToWideChar(CP_UTF8, , psText, -, pwText, dwNum); return ;
}

CString is an ATL/MFC class (actually, a specialization of the CStringT class template). Because ATL and MFC are Windows specific, the class is also inherently Windows specific. ATL and MFC are not included with VC++ Express, so if you don't have VC++ Professional or better, using this isn't an option for you.

If you want a string class that is platform agnostic, use std::string or std::wstring (which are specializations of the std::basic_string class template) instead, as these are part of the C++ standard library.

  http://social.msdn.microsoft.com/Forums/zh-CN/660029ba-994a-4f85-871b-2e5ae7b9c95b/cstring-help-in-regular-c?forum=vcgeneral

But, if you are going to write Win32 C++ code, I think CString's interface is more convenient (...but maybe someone would say it is more "bloated" ;)

For example: with CString you have methods to load strings from the resources.

Moreover, CString offers a convenient FormatMessage method, which is good for internationalization, see the so called problem of "Yoda speak" on Mihai Nita's blog post here:

http://mihai-nita.net/2006/04/15/string-api-and-internationalization/

And CString assumes that strings are NUL-terminated (which is good for interoperability with Win32 functions like say GetWindowText), instead std::[w]string doesn't.

And CString offers an implicit conversion operator LPCTSTR, so you can simply pass CString's to Win32 APIs having LPCTSTR parameters.

Moreover, if you are using a pre-VC10 compiler which does not support move semantics, I think storing CString in STL containers and passing them around is faster than std::[w]string, because CString uses COW (Copy-On-Write) technique, so it avoids useless copies (e.g. when a vector is resized because its capacity is insufficient). (However, I think this problem is solved in VC10 thanks to move semantics applied to std::[w]string.)

C++:在非MFC程序中如何引用CString?

http://blog.csdn.net/xiashengfu/article/details/7911086

上一篇:NBUT 1673 迷宫问题(DP)


下一篇:iOS开发——多线程篇——NSThread