vc++数据类型转换(Vc++ data type conversion)
It (2009-07-23 15:59:46) tags used in VC++: Category: Programming
String conversion
1.CString transforms into plastic Int
CString str=_T ("12345");
ATOI ((LPCSTR) STR); / / LPCSTR const char* can be transformed into
CString str= "1"";
Int n=atoi (str.GetBuffer (0));
2. transfer a CString to an array of char (char*)
Char buffer[128]
CString str;
A.//strcpy method
Strcpy (buffer, str.GetBuffer ());
Str.ReleaseBuffer ();
B.// forced conversion method
Buffer= (LPTSTR) (LPCTSTR) str;
C.//sprintf method
Sprintf (buffer,%s, STR);
D.
CString str;
Int, nLength=str.GetLength ();
Char * sz=new char[nLength];
Sz=str.GetBuffer (0);
(LPCSTR) CString
E. can be converted to char* by type forcing, such as CString: CString cStr = "Hello, world"!";
Char* zStr = (char*) (LPCTSTR) cStr;
3.int turn CString
CString string;
Int iValue=100;
String.Format (_T ("%d"), iValue);
MessageBox (string);
The value of "string" is "100""
4.char * turn CString
A.
Char sz[128];
CString str;
Str.Format ("%s", SZ);
B.
CString.format ("%s", char*);
CString strtest;
Char * charpoint;
Charpoint= "give string a value"";
Strtest=charpoint; / / direct payment value
C.
字符串是什么类型的
The char* type can be given directly to the CString and complete automatic conversion, for example:
Char* zStr = "Hello, world"!";
CString cStr = zStr;
5.Float turn CString
Float f=0.0;
CString str;
Str.Format ("%f", f);
6.CString turn Float
CString str= "0"";
Float f=atof (str.GetBuffer (0));
7.string turn CString
CString.format ("%s", string.c_str ()); Using c_str () is really better than data () 8.char* turn 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);
9.CString turn string
String s (CString.GetBuffer ());
GetBuffer () must be ReleaseBuffer (), otherwise there is no space for the buffer to be released
10.int turn char *
There is a function Itoa () in stdlib.h
The use of itoa:
Itoa (I, num, 10);
I needs to convert characters into numbers
Save character variables after num conversion
10 conversion of the number of base (hexadecimal) 10, that is, in accordance with the 10 hexadecimal conversion numbers. Can also be 2, 8, 16, etc., you like the hexadecimal type
Char *itoa (int, value, char*, string, int, Radix);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论