String, char *, int type conversion, C ++ forced conversion

Source: Internet
Author: User
The following are commonly used conversions between several types to convert 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 ); you can use the stringstream class (including the <sstream> header file) int main () {stringstream SS; int I = 100; string STR; SS> I; SS <STR; // in this case, STR is the string "100 ". retturn 0 ;}............................. char * to string S (char *); your class can only be initialized. It is best to use assign () convert to char * In stdlib if it is not initialized. h: ITOA (I, num, 10 ); I need to convert the numeric num to the character, and then save the variable 10 of the character to convert the numeric base (in hexadecimal) 10, that is, convert the number in hexadecimal notation. It can also be 2, 8, 16, etc. You like the base type 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 * 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 ());
Related Article

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.