Knowledge record: String, wstring, cstring, Char, tchar, Int, DWORD Conversion Method

Source: Internet
Author: User
Programming has been a headache recently. This type of conversion can be done by knowing that it can be switched, but it is always hard to remember. I will keep searching through the Internet and summarize it here. For future convenience, of course, some methods may not be the latest or the simplest, but they should be much more convenient to use as you know:
1 "string to wstring
wstring s2ws(const string& s)
{
 _bstr_t t = s.c_str();
 wchar_t* pwchar = (wchar_t*)t;
 wstring result = pwchar;
 return result;
}
2. Convert wstring to string
string ws2s(const wstring& ws)
{
 _bstr_t t = ws.c_str();
 char* pchar = (char*)t;
 string result = pchar;
 return result;
}
3 "string to cstring
a)CString.format("%s", string.c_str());  

 

b)CString StringToCString(string str)
{
CString result;
for (int i=0;i<(int)str.length();i++)
{
 result+=str[i];
}
return result;
}

 

4. Convert cstring to string
a)void ConvertCString2string(CString& strSrc,std::string& strDes)
{
#ifndef UNICODE
 strDes = strSrc;
#else USES_CONVERSION;
 strDes = W2A(strSrc.LockBuffer());
 strSrc.UnlockBuffer();
#endif
}
b)
string s(CString.GetBuffer());  
ReleaseBuffer();
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.
c)
string CStringToString(CString cstr){string result(cstr.GetLength(),'e');for (int i=0;i<cstr.GetLength();i++){ result[i]=(char)cstr[i];}return result;}
5. Convert string to char *
a)char *p = string.c_str();
Example: String AA ("AAA"); char * c = AA. c_str (); string mngname; char T [200]; memset (T, 0,200); strcpy (T, mngname. c_str ());
B) Assignment of one character

 

Char * P = new char [sring Length + 1];
P [String Length] = '/0 ';
But pay attention to the final value '/0 '!!!

 

char * StringToChar(string &str)
{
int len=str.length();
char * p= new char[len+1];
for (int i=0;i<len;i++)
{
p[i]=str[i];
}
p[len]='/0';
}
6. Convert char * to string
String S (char *); your class can only be initialized. It is best to use assign () unless it is initialized ();
string CharToString(char*arr,int count){string result(arr,4);return result;}
String is an ANSI-encoded char.
Tchar is a Unicode Character wchar_t
7. Convert string to tchar *
/* Declare wbuf as a pointer. */Wchar_t * chr2wch (const char * buffer) {size_t Len = strlen (buffer); size_t WLEN = multibytetowidechar (cp_acp, 0, (const char *) buffer, INT (LEN), null, 0); wchar_t * wbuf = new wchar_t [WLEN + 1]; multibytetowidechar (cp_acp, 0, (const char *) buffer, INT (LEN ), wbuf, INT (WLEN); Return wbuf ;}
8. Convert tchar * to string
char * wch2chr(LPCTSTR lpString){// Calculate unicode string length.UINT len = wcslen(lpString)*2;char *buf = (char *)malloc(len);UINT i = wcstombs(buf,lpString,len);return buf;}
9. Convert string and char * to int
String to int .............................. char * to int # include <stdlib. h> int atoi (const char * nptr); long atol (const char * nptr); long Atoll (const char * nptr); long atoq (const char * nptr );
10. Convert int to char * and string.
In stdlib. h, there is a function ITOA () ITOA usage: ITOA (I, num, 10); I need to be converted to the numeric num of the character to save the variable of the character
11. Convert wstring to csting
STD: wstring to cstring
CString str( filename.c_str() ); 
12. Convert cstring to wstring
Cstring to STD: wstring
std::wstring str = filename.GetString();
13. Convert cstring to char *

Cstring CSTR (asdd );

const char* ch = (LPCTSTR)cstr;
Example:

Cstring STR = "I am good ";

char*   lp=str.GetBuffer(str.GetLength()); 
str.ReleaseBuffer(); 

14. Convert char * To cstring

Example:

Cstring STR;

char   pStr[100]; 
str.Format( "%s ",pStr);
15 tchar to Char
*********************************************************************** 
* Function: thcar2char
* Description: Convert tchar * To char *
*********************************************************************** 
char* CPublic::THCAR2char(TCHAR* tchStr) 
Int ilen = 2 * wcslen (tchstr); // cstring. The length of a tchar character is not required.
char* chRtn = new char[iLen+1] 
Wcstombs (chrtn, tchstr, ilen + 1); // If the conversion is successful, a non-negative value is returned.
return chRtn; 

16. Convert Char to tchar

After the Unicode macro is defined, tchar is the wide character wchar_t. Otherwise, tchar and Char are the same ^ _

 

Specific analysis of specific problems, float cloud, everything is float cloud .....
The following is an excerpt from the network:
..............................................................
As mentioned in C ++ standard function library
There are three functions that can convert the content of a string to a character array and a C-string
1. Data (), returns a string array without "/0"
2, c_str (), returns a string array with "/0"
3,copy() 
.................................................................
Int to cstring:
CString.Format("%d",int);
...............................
String to cstring
CString.format("%s", string.c_str());  
C_str () is indeed better than data.
.......................................
Char * To cstring
CString.format("%s", char*); 
 CString strtest;  
 char * charpoint;  
 charpoint="give string a value";  
Strtest = charpoint; // pay the value directly
...................................................................
Cstring to int
 CString  ss="1212.12";  
Int temp = atoi (SS); // atoi _ atoi64 or atol
  
Converts a character to an integer. You can use atoi, _ atoi64, or atol.
int int_chage = atoi((lpcstr)ss) ;
Or:
   CString str = "23";
   UINT uint;
   sscanf(str, "%d", uint);
..............................
String to int
..............................
Char * to int
 #include <stdlib.h> 
  
 int atoi(const char *nptr); 
 long atol(const char *nptr); 
 long long atoll(const char *nptr); 
 long long atoq(const char *nptr); 
...................................................................
Cstring to string
  string s(CString.GetBuffer());  
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.
..........................................
Int to string
..........................................
Char * to string
 string s(char *);  
You can only initialize it. It is best to use assign () unless it is initialized ().
...................................................................
Convert cstring to char *
 CString strtest="wwwwttttttt";
 charpoint=strtest.GetBuffer(strtest.GetLength());
Cstring conversion char [100]
 char a[100];  
 CString str("aaaaaa");  
 strncpy(a,(LPCTSTR)str,sizeof(a));
  CString  str="aaa";   
  char*  ch;   
  ch=(char*)(LPCTSTR)str;
..........................................
Convert int to char *
There is a function ITOA () in stdlib. h ()
ITOA usage:
 itoa(i,num,10); 
I number to be converted to characters
Variable for saving characters After num Conversion
10: The base (in hexadecimal) 10 of the number to be converted. It can also be 2, 8, 16, and so on.
Prototype: char * ITOA (INT value, char * string, int Radix );
Instance:
 #include "stdlib.h" 
 #include "stdio.h" 
 main() 
 { 
 int i=1234; 
 char s[5]; 
 itoa(i,s,10); 
 printf("%s",s); 
 getchar(); 
}
..........................................
String to char *
char *p = string.c_str();  
 
 string aa("aaa"); 
 char *c=aa.c_str();
 string mngName; 
 char t[200]; 
 memset(t,0,200); 
 strcpy(t,mngName.c_str());
...................................................................
There is no string in Standard C, char * = char [] = string
You can use the cstring. Format ("% s", char *) method to convert char * To cstring. Convert cstring to char *.
You can use the operator (lpcstr) cstring.
cannot convert from 'const char *' to 'char *' 
const char *c=aa.c_str();  
String. c_str () can only be converted to const char *


 
 
 
 
 
 
 
 
 
 
#include <string>
// You must use MFC to use cstring and cannot contain <windows. h>
#define _AFXDLL
#include <afx.h>
using namespace std;
//———————————————————————————-
// Convert single-byte char * to wide-byte wchar *
inline wchar_t* AnsiToUnicode( const char* szStr )
{
int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
if (nLen == 0)
{
   return NULL;
}
wchar_t* pResult = new wchar_t[nLen];
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
return pResult;
}
//———————————————————————————-
// Convert the wide byte wchar_t * to a single byte char *
inline char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
   return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
return pResult;
}
//———————————————————————————-
// Convert a single-character string to a wide-character wstring
inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr )
{
int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );
wszStr.resize(nLength);
LPWSTR lpwszStr = new wchar_t[nLength];
MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength );
wszStr = lpwszStr;
delete [] lpwszStr;
}
//———————————————————————————-
int _tmain(int argc, _TCHAR* argv[])
{
Char * pchar = "I like char ";
Wchar_t * pwidechar = l "I hate wchar_t ";
wchar_t   tagWideCharList[100] ;
char   ch = ‘A’;
char   tagChar[100] = {NULL};
CString   cStr;
std::string str;



// Note: Set the language environment to output widechar
setlocale(LC_ALL,”chs”);



// Note: char * converts wchar_t *
// Note: wchar_t is not overloaded <, so cout cannot be used <output
pWideChar = AnsiToUnicode( pChar );
// Note: printf ("% ls") and wprintf (L "% s") are consistent
printf( “%ls/n”, pWideChar );



// Note: wchar_t * converts wchar_t []
wcscpy ( tagWideCharList, pWideChar );
wprintf( L”%s/n”, tagWideCharList );



// Note: wchar_t [] converts wchar_t *
pWideChar = tagWideCharList;
wprintf( L”%s/n”, pWideChar );



// Note: Char converts string
str.insert( str.begin(), ch );
cout << str << endl;



// Note: wchar_t * converts string
pWideChar = new wchar_t[str.length()];
swprintf( pWideChar, L”%s”, str.c_str());
wprintf( L”%s/n”, pWideChar );



// Note: String Conversion char *
pChar = const_cast<char*>(str.c_str());
cout << pChar << endl;



// Note: char * converts string
str = std::string(pChar);
// Note: cout <reloads string. If printf is used, printf ("% s", str. c_str () is required ());
// But cannot print ("% s", STR); Because STR is a string class
cout << str << endl;



// Note: String Conversion char []
STR = "Bored ";
strcpy( tagChar, str.c_str() );
printf( “%s/n”, tagChar );



// Note: String Conversion cstring;
cStr = str.c_str();



// Note: cstring conversion string
str = string(cStr.GetBuffer(cStr.GetLength()));



// Note: char * converts cstring
cStr = pChar;



// Note: cstring converts char *
pChar = cStr.GetBuffer( cStr.GetLength() );



// Note: cstring converts char []
strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));



// Note: cstring conversion wchar_t *
pWideChar = cStr.AllocSysString();
printf( “%ls/n”, pWideChar );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.