C++ : How to convert from std::string to CString?

Q: How to convert from std::string to CString?

A: This is simple... Use the CString constructor.

std::string strStdString ("Hello!");
// Using CString Constructor
CString strCString (strStdString.c_str ());

Let me add that CStringT can construct from both character or wide-character strings. i.e. It can convert from char* (i.e. LPSTR) or from wchar_t* (LPWSTR).

In other words, char-specialization (of CStringT) i.e. CStringA, wchar_t-specilization CStringW, and TCHAR-specialization CString can be constructed from either char or wide-character, null terminated (null-termination is very important here) string sources.

Q: How to convert from CString to std::string?

A: There are really many ways to do it.

But, the simplest one is just this -

CString strSomeCstring ("This is a CString Object");
// Use ANSI variant CStringA to convert to char*; construct from it -
std::string strStdString (CStringA (strSomeCstring));

CStringA is a template specialization of class CStringT for type char avaílable with Visual Studio 7.x and better.

Q: What are correlations between the CString Family and the Standard String Family?

A: If we were to draw a correlation between the CString Family and Standard String Classes, we would notice -

  1. CStringT as the equivalent of std::basic_string
  2. CStringA as the equivalent of std::string
  3. CStringW as the equivalent of std::wstring

However, the standard library itself doesn't contain a "CString" equivalent - one that can at best be -

std::basic_string <TCHAR>

This makes CString (VS 7.x +) "cool".

Q: The history of CString...

A: CString really wasn't always as likeable as it is today. If one were to compare the CString of Visual Studio 7.x and better with the class supplied by MSVC 6.0, one might not be wrong in judging the erstwhile version as relatively inflexible, and restrictive.

What made CString of yesterday "inflexible"?

1.Only for MFC-folks!

Yes... CString with MSVC 6.0 was notoriously connected to MFC Projects as if other project types didn't have any need for a TCHAR String Wrapper Class! One reason for it would be the linker error one would get for using this class in a non-MFC Project.

2. Need to use a CString? Convert to TCHAR!

CString is a string wrapper for TCHAR strings. In doing so, it came with a good number of helpful member methods. However, the implementation of this class in MSVC 6.0 was such that if a programmer needed to make use of the utility member methods, he would need to convert his string data type to TCHAR - back and forth.

This "restriction" automatically excluded programmers using ANSI strings (char*) in UNICODE builds from using this class, and ditto for those using UNICODE strings (WCHAR*) in non-UNICODE builds from using this class - as in both these cases, the strings in question aren't TCHAR strings - the type that CString wraps.

What makes CString of today "flexible" and actually more "likeable"?

1. Use it in any project type - even in a console application!

Yes... All the programmer needs to do is to include the following header -

#include <atlstr.h>

 

2. Don't change your String Type to suit CString! Choose the right CString instead!

With Visual Studio 7.x and better, CString is actually a specialization of template Class CStringT for type T as TCHAR.
Additionally, there exist two useful specializations of CStringT -

  • CStringA that specializes CStringT for type char, and
  • CStringW that specilizes CStringT for type wchar_t.
Hence, CString is actually CStringA for a non-UNICODE build and is a CStringW for a UNICODE build.

The existence of CStringA and CStringW gives the programmer, the flexibility to use the utility member methods supplied by CStringT even when their strings aren't of type - TCHAR.

Note: Most of the material in this article is taken from codeguru.com


Recommended Reading :

Comments

people

phentermine iv adipex phentermine diet phentermine pill xenical buy phentermine without prescription phentermine no rx needed phentermine 37.5 mg about phentermine phentermine cheap buy phentermine on line purchase phentermine without prescription phentermine worldwide phentermine purchase where to buy phentermine 52hepipl buy link phentermine phentermine order phentermine round tablet phentermine online purchase cheap phentermine free shipping phentermine diet pills phentermine on line prescription phentermine adipex vs phentermine buy phentermine buy phentermine online buy phentermine without prescription buy phentermine on line where to buy phentermine buy cheap phentermine 52hepipl buy link phentermine buy cod phentermine rx shipping buy phentermine uk buy cheap phentermine online buy phentermine cheap phentermine buy buy in mo phentermine buy phentermine fed ex buy phentermine zsupl phentermine buy online buy phentermine adipex online cheap phentermine buy buy in phentermine uk buy high-quality phentermine buy card master phentermine buy online phentermine adipex buy generic phentermine buy chip phentermine buy link phentermine siteneogenro buy phentermine online com where buy phentermine adipex buy phentermine buy phentermine online for cheap where can i buy phentermine best place to buy phentermine without prescription buy phentermine where buy phentermine best online pharmacy valium buy valium valium online online valium generic valium order valium buy valium online effects side valium valium side effects online order valium pill valium mortgage valium tablet valium order valium online valium dosage valium addiction valium line prescription valium without online pharmacy valium purchase valium valium sale dj valium picture valium drug test valium online purchase valium purchase valium online roche valium valium withdrawal valium overdose effects valium valium effects cheap valium valium vs xanax buy cheap valium dose valium valium mexico dosage valium diazepam valium xanax valium valium pharmacy side effects of valium and diazepam drug valium does in long stay system valium valium 10 valium for dog buy overnight valium valiums buy valium without prescription 4.01 line valium 3.75 discount valium discount valium use valium 3.75 order valium 10mg 3.54 valium suicide valium by valium valium com blue valium 3.53 buy valium valium pregnancy herbal valium poker valium how long does valium stay in your system delivery overnight valium valium buy valium valium online cheap valium discount valium where can i buy valium order buy valium free shipping valium side effects buy valium online order valium online diazepam generic valium valium diazepam generic valium online pharmacy valium valium no prescription xanax valium order valium valium pill cheap tramadol purchase valium buy cheap valium buy valium where buy diazepam valium length of valium valium pictures valium rx generic cheap-valium valium vs ativan valium for sale valium effects valium without prescription how to get valium prescribed to you online valium description of valium 5mg valium prescription without anafen valium buying valium online buying valium purchase valium online valium 180 buy viagra where to buy viagra where to buy viagra on line buy viagra online buy herbal viagra buy viagra in the uk buy viagra in canada buy viagra now buy viagra low cost buy viagra cheap buy viagra on line buy generic viagra buy cheap viagra buy viagra internet cheapest place to buy viagra online where to buy viagra from buy discount online viagra buy viagra pill buy cialis viagra where can i buy viagra uk buy generic viagra online buy com lvivhost online viagra buy lady uk viagra best buy viagra buy cheap cialis generic levitra viagra 4.24 buy viagra www buy viagra buy cialis online viagra buy internet viagra buy viagra now online viagra buy buy viagra online and get prescription buy viagra online cheap buy discount viagra online buy viagra uk viagra buy in uk online buy online viagra viagra viagra alternative generic viagra female viagra viagra sample cheap viagra

people

valium buy valium valium online online valium generic valium order valium buy valium online effects side valium valium side effects online order valium pill valium mortgage valium tablet valium order valium online valium dosage valium addiction valium line prescription valium without online pharmacy valium purchase valium valium sale dj valium picture valium drug test valium online purchase valium purchase valium online roche valium valium withdrawal valium overdose effects valium valium effects cheap valium valium vs xanax buy cheap valium dose valium valium mexico dosage valium diazepam valium xanax valium valium pharmacy side effects of valium and diazepam drug valium does in long stay system valium valium 10 valium for dog buy overnight valium valiums buy valium without prescription 4.01 line valium 3.75 discount valium discount valium use valium 3.75 order valium 10mg 3.54 valium suicide valium by valium valium com blue valium 3.53 buy valium valium pregnancy herbal valium poker valium how long does valium stay in your system delivery overnight valium valium buy valium valium online cheap valium discount valium where can i buy valium order buy valium free shipping valium side effects buy valium online order valium online diazepam generic valium valium diazepam generic valium online pharmacy valium valium no prescription xanax valium order valium valium pill cheap tramadol purchase valium buy cheap valium buy valium where buy diazepam valium length of valium valium pictures valium rx generic cheap-valium valium vs ativan valium for sale valium effects valium without prescription how to get valium prescribed to you online valium description of valium 5mg valium prescription without anafen valium buying valium online buying valium purchase valium online valium 180

people

xanax buy xanax cheap xanax buy xanax online buy online xanax buy line xanax buy xanax on line diet pill xanax online prescription xanax order xanax effects side xanax generic xanax xanax bar picture of xanax online xanax withdrawal xanax xanax withdrawal xanax side effects xanax prescription tablet xanax addiction xanax xanax addiction xanax xr order xanax online online order xanax prescription xanax symptom withdrawal xanax alprazolam xanax alprazolam effects side xanax xanax abuse abuse xanax overdose xanax xanax overdose dosage xanax xanax dosage xanax information xanax online pharmacy online pharmacy xanax does in long stay system xanax purchase xanax overnight xanax pregnancy xanax xanax com how long does xanax stay in your system buy generic xanax 2mg xanax suicide xanax xanax online xanax drug test drug test xanax xanax pill pill xanax xanax alcohol discount xanax site submit web xanax xanax medication alcohol xanax yellow xanax bar bar xanax yellow valium vs xanax snorting xanax ativan vs xanax buy cheap xanax xanax and pregnancy cheapest xanax white bar xanax xanax and grapefruit grapefruit xanax drug xanax xanax drug medication xanax xanax us pharmacy does like look xanax xanax valium link xanax alprazolam generic xanax xanax upjohn discount online xanax xanax ativan buy pharmacy xanax xanax description consultation free online xanax purchase xanax online xanax ambien ativan xanax xanax and weight gain gain weight xanax xanax half life xanax ts xanax used for xanax cheap buspar xanax xanax withdrawl xanax 1mg xanax bars xanax prescription online united states xanax online no rx buying xanax online xanax onlune pharmacy ac buy xanax alprazolam buy xanax xanax no script buy xanax without prescription bar xanax withdrawls xanax xanax withdrawal symptoms purchasing xanax xanax side effect how long before xanax addiction xanax effects




Loans - Credit Card - New York Hotels - Internet Marketing