C ++ conversion of various types

Source: Internet
Author: User

1.
Conversion between int and char:
Int atoi (const char * Str );
Example
Char * Ch = "152 ";
Int num = atoi (CH );
Output: num = 152;
Char * ITOA (INT Val, char * dstbuf, int Radix );

Example

Int number = 123456789;

Char string [25];

Char * Ch;
Ch = ITOA (number, String, 10 );
Output: CH = "123456" string = "123456"
2. Long and char * Conversions
Char * ltoa (long _ Val, char * _ dstbuf, int _ Radix );
Long atol (const char * Str );
3. Double and char * Conversion
Double atof (const char * string );
There is no corresponding ftoa, so sprintf is used for implementation.

Char ch [50];

Double DBL = 12345.123;
Sprintf (CH, "% F", DBL );
Output: CH = "12345.1230000"
4. Conversion between char * And wchar_t *
Size_t mbstowcs (wchar_t * wcstr, const char * mbstr, size_t count );

Char * Ch;
Ch = "I am student ";

Wchar_t wch [50];

Int size = mbstowcs (wch, CH, 13 );
Output: size = 12; wch = "I am student ";

Wchar_t * wch = l "I am student ";

Char ch [50];

Int size = wcstombs (CH, wch, 13 );
Output: size = 12; CH = "I am student ";
5.
Const char * is converted to char *. This transformation is often used between string and char *. String. c_str () returns const char *
Const_cast <char *> (const char *)
6.
Chinese character problem. When fstream opens a file, the file cannot be opened if the file path on vs2008 or vs2003 contains Chinese characters. Solution:
Locale: Global (locale (""); // before opening the file
Fstream. Open (...);
Locale: Global (locale ("C"); // After the file is opened, if you do not use this statement to restore the default settings, garbled code will appear in the subsequent cout.
7.
Character Set conversion: lpstr lpwstr
Convert lpwstr to lpstr
Lptstr pp = l "I Am a strudent ";
Lpstr P = new char [100];
Widechartomultibyte (cp_acp, 0, PP,-1, P, 100, null, null );
Output: P = "I Am a strudent ";
Lpstr to lpwstr
Lpwstr pp = new tchar [100];
Lpstr P = "I Am a strudent ";
Multibytetowidechar (cp_acp, 0, P,-1, PP, 100 );
Output: pp = "I Am a strudent ";
Note: If the value of the first parameter in widechartomultibyte and multibytetowidechar is set to zero, no conversion is performed, and the size of the storage zone required for the conversion is directly returned. Therefore, it is often used as follows:
Lpstr P = "I Am a strudent ";
Int Mm = multibytetowidechar (cp_acp, 0, P,-1, null, 0 );
Lpwstr pp = new tchar;
Multibytetowidechar (cp_acp, 0, P,-1, PP, mm );
Delete [] pp;
8. Get the current time and convert it to a string:
Time_t ltime;
Ltime = time (0 );
Char TMP [64];
Zeromemory (TMP, 0 );
Strftime (TMP, sizeof (TMP), "% Y/% m/% d % x", localtime (& ltime ))
Output: TMP = "10:14:07 ";
9. The conversion between cstring, lpstr, and lpwstr is not described here. Cstring is automatically adjusted to cstringw according to different project settings. cstringa corresponds to lpwstr and lpstr respectively, and can be directly transferred under corresponding conditions. Therefore, the fundamental problem encountered is the conversion between lpstr and lpwstr. For more information about cstring conversion, see. If you find that the cstring conversion method on the Internet is not available to you, you don't have to complain about them, but their project settings are different from yours. For more information, see 7.
10. Supplement: static_cast <> ()
Const_cast <> () reinterpret_cast <> dynamic_cast <> (). These four specific uses can be Google, and their role is also very elegant in many cases.

The type conversion of C ++ is a serious problem that can drive people crazy. In many cases, they have to spend time solving these details. The situations listed in this article are limited after all and do not involve any omnipotent type. I hope you can read this article and add it. I hope that later articles and comments in this article can solve most of the type conversion problems. Use it as a Tool Book.
The potential purpose of writing this article is to write a class, which includes various common types of conversions to solve these essential problems that you spend unnecessary effort on coding.

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.