VC ++ basic knowledge

Source: Internet
Author: User
Tags decimal to hex hex to decimal
ArticleDirectory
    • Decimal to hex
    • HEX to decimal
    • Decimal to time
    • String to hex
    • HEX to cstring
    • Colevariant to cstring
    • Cstring to Char pointer
    • Char pointer to cstring
    • Double to cstring including the fractional part
    • Double to cstring
    • Cstring to double
    • Cstring to lpcstr
    • Cstring to lpstr
    • Cstring to wchar *
    • Lptstr to lpwstr
    • String to BSTR
    • Cstring to BSTR
    • _ Bstr_t to cstring
    • Char array to integer
    • Char array to float
    • Char pointer to double
    • Char pointer to integer
    • Char pointer to long
    • Char * To BSTR
    • Float to word and vice versa

1. The symbols are not described as follows: Scope delimiters;. Category class members;-> pointers.

2. wide range of types:


Lpstr 32-bit pointer, pointing to a string
The 32-bit pointer to a constant string. This string can be transplanted to Unicode and DBCS (dubyte Character Set) and points to the const type string pointer per character 16 bytes.
Lptstr 32-bit pointer, pointing to a string, which can be transplanted to Unicode and DBCS (dubyte Character Set), pointing to the pointer of a string of 16 bytes per character.
The 32-bit pointer to a constant string and a pointer to a const string of every 8 bytes.
Unicode string ending with \ 0, that is, double byte

 

Windows data types are described as follows:
Windows Data Types
Lpcstr: pointer to a constant null-terminated string of 8-bit windows (ANSI) characters.
Lpctstr: An lpcwstr if Unicode is defined, an lpcstr otherwise.
Lpcwstr: pointer to a constant null-terminated string of 16-bit Unicode characters.
Lptstr: An lpwstr if Unicode is defined, an lpstr otherwise.
Lpstr: pointer to a null-terminated string of 8-bit windows (ANSI) characters.
Lpwstr: pointer to a null-terminated string of 16-bit Unicode characters.

 

Char 8bit ANSI character
Wchar 16bit Unicode Character
If tchar defines Unicode macros (# def Unicode), tchar is equivalent to wchar. If no Unicode macro is defined,
Tchar is equivalent to Char

 

Lpcstr: equivalent to const char * read-only ANSI string pointer
Cstring: the class for packaging strings in MFC, which can be ANSI or uncode.
Char * ANSI string pointer
Char [] ANSI character array, which can be used as a pointer string
Char is equivalent to Char, an ANSI character

 

L ("hello") indicates converting to a wide character to support Unicode

3. type conversion:

Decimal conversionsdecimal to hex
 
//Use _ ITOA () function and set Radix to 16.CharHexstring [10];IntNumber =30; ITOA (number, hexstring,16);//In hexstring is 1E.
HEX to decimal
 
//You can use strtol function and you can specify base.Char* Hexstring ="Abcdef";Char* P;IntNumber = strtol (hexstring, & P,16);//A function that does thisBoolHextodecimal (Char* Hexnumber, Int & number ){Char* Pstopstring; number = strtol (hexnumber, & pstopstring,16);Return(Bool) (Number! = Long_max );}
Decimal to time
Char* Dectotime (FloatFtime,Char* Sztime ){IntNhrs, Nmin, nsec; ftime * =3600; Nhrs = (Int) Ftime/3600; Nmin = (Int) (Ftime-nhrs *3600)/60; Nsec = (Int) (Ftime-nhrs *3600-Nmin *60); Wsprintf (sztime,"% 02d. % 02d. % 02d hrs. Min. Sec .", Nhrs, Nmin, nsec );ReturnSztime ;}
String conversionsstring to hex
Sscanf (String, % 04x, & your_word16 );//Where string = your string and//04 = length of your string and x = hex
HEX to cstring
 
Cstring STR;Unsigned CharWrite_buff [1]; Write_buff [0] = 0x01; Str. Format ("0x0% x", Write_buff [0]);
Colevariant to cstring
 
Cstring strtemp; colevariant var; Var ="Firstname"; Strtemp = var. bstrval; afxmessagebox (strtemp );
Cstring to Char pointer
Cstring mystring ="Abcdef";Char* Szmystring = (Char*) (Lpctstr) mystring;
 
Char* Pbuffer =New Char[1024]; Cstring strbuf ="Test"; Pbuffer = strbuf. getbuffer (Sizeof(Pbuffer ));
Char pointer to cstring
 
Char* Mystring ="12345"; CstringString= Mystring;
Double to cstring including the fractional part
Cstring strvalue, strint, strdecimal; Int Decimal, sign; Double Dvalue = 4 . 125 ; Strvalue = _ fcvt (dvalue, 6 , & Decimal, & sign ); //  Now decimal contains 1 because there is      //  Only one digit before. Strint = strvalue. Left (decimal ); //  Strint contains 4 Strdecimal = strvalue. mid (decimal ); //  Strdecimal contains 125. Cstring strfinalval; strfinalval. Format ( "  % S. % s" , Strint, strdecimal ); //  Strfinalval contains 4.125 
Double to cstring
 
Cstring strvalue;IntDecimal, sign;DoubleDvalue =123456789101112; Strvalue = _ ECVT (dvalue,15, & Decimal, & sign );
Cstring to double
 
Strvalue ="121110987654321"; Dvalue = atof (strvalue );
Cstring to lpcstr
Cstring str1 = _ T ("My string");IntNlen = str1.getlength (); lpcstr lpszbuf = str1.getbuffer (nlen );//Here do something with lpszbuf ...........Str1.releasebuffer ();
Cstring to lpstr
 
Cstring STR = _ T ("My string");IntNlen = Str. getlength (); lptstr lpszbuf = Str. getbuffer (nlen );//Here do something with lpszbuf ...........Str. releasebuffer ();
Cstring to wchar *
Cstring STR ="A string here"; Lpwstr lpszw =NewWchar [255]; Lptstr lpstr = Str. getbuffer (Str. getlength ());IntNlen = multibytetowidechar (cp_acp,0, Lpstr ,-1, Null, null); multibytetowidechar (cp_acp,0, Lpstr ,-1, Lpszw, nlen); afunctionuseswchar (lpszw );Delete[] Lpszw;
Lptstr to lpwstr
 
IntNlen = multibytetowidechar (cp_acp,0, Lptstr ,-1, Null, null); multibytetowidechar (cp_acp,0, Lptstr ,-1, Lpwstr, nlen );
String to BSTR
StringSs ="Girish"; BSTR _ bstr_home = a2bstr (ss. c_str ());
Cstring to BSTR
 
Cstring STR ="Whatever"; BSTR resultsstring = Str. allocsysstring ();
_ Bstr_t to cstring
# Include <Ansiapi. h># Include <Comdef. h>_ Bstr_t bstext ("Hai Bayram"); Cstring strname; w2a (bstext, strname. getbuffer (256),256); Strname. releasebuffer (); afxmessagebox (strname );CharSzfilename [256]; Getmodulefilename (null, szfilename,256); Afxmessagebox (szfilename );
Character arrayschar array to integer
CharMyarray [20];IntNvalue; nvalue = atoi (myarray );
Char array to float
 
CharMyarray [20];FloatFvalue; fvalue = atof (myarray );
Char pointer to double
 
Char* STR ="-343.23";DoubleDval; dval = atof (STR );
Char pointer to integer
 
Char* STR ="-343.23";IntIval; ival = atoi (STR );
Char pointer to long
Char* STR ="99999";LongLval; lval = atol (STR );
Char * To BSTR
 
Char* P ="Whatever"; _ Bstr_t BSTR = P;
Float to word and vice versa
FloatFvar; Word wvar; fvar =247.346; Wvar = (Word) fvar;//Converting from float to word.//The value in wvar wocould be 247Wvar =247; Fvar = (Float) Fvar;//Converting from word to float.//The value in fvar wocould be 247.00

 

 

 

 

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.