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

C++ string的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

随机推荐

  1. Android 一些基本组件的使用

    Dialog 基本用法 ,带自定义view AlertDialog dialog = new AlertDialog.Builder(context).setTitle("写入信息" ...

  2. 28. 字符串的排列之第1篇[StringPermutation]

    [题目] 输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab和cba. [分析] 这是一道很好的 ...

  3. float 比较, 这是一个坑

    为了方便随机关键产品数据,做了一个随机值列的方案,列字段类型设置为float. 在测试的两个随机值的时候, 故意设置了几个随机值相同保存到数据库表中, 这样问题就出来了. 详细如下: 当进行小于比较的 ...

  4. 常用语句if,for,while

    一.变量赋值 a = 3 b = a a = 5 print a,b 5,3   变量命名规则:   1.显式 2.nums_of_alex_gf = 19 3.NumsOfAlexGf = 2 4. ...

  5. hive的数据导出方式

    hive有三种导出数据的方式 >导出数据到本地 >导出数据到hdfs >导出数据到另一个表   导出数据到本地文件系统 insert overwrite local director ...

  6. 利用python的双向队列(Deque)数据结构实现回文检测的算法

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  7. 最短路径—Dijkstra算法和Floyd算法

    原文链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最后边附有我根据文中Dijkstra算法的描述使用jav ...

  8. 提交form表单

    方法一: $.ajax({ }) $.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid' ...

  9. 意法STM32F1系列MCU单片机解密芯片破解复制

    意法STM32F1系列MCU单片机解密芯片破解复制 STM32F1系列MCU芯片解密: STM32F100解密 | STM32F101解密 | STM32F102解密 | STM32F103解密 |  ...

  10. zookeeper原理及作用

    ZooKeeper是Hadoop Ecosystem中非常重要的组件,它的主要功能是为分布式系统提供一致性协调(Coordination)服务,与之对应的Google的类似服务叫Chubby.今天这篇 ...