c语言330转化成字符,c/字符串,字符转数字,数字转字符(转)
一.將CString轉為CTime的幾種方法
CString ?timestr
= ?"2000年04月05日";
int ?a,b,c ?; ?sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c);
CTime ?time(a,b,c,0,0,0);?--------or - ---------------------
CString ?s("2001-8-29
19:06:23"); ?int ?nYear, ?nMonth, ?nDate, ?nHour,
nMin, ?nSec;
sscanf(s, ?"%d-%d-%d
%d:%d:%d", ?&nYear, ?&nMonth,
&nDate, ?&nHour, ?&nMin,
&nSec); ?CTime ?t(nYear,
nMonth, ?nDate,
nHour, ?nMin,
nSec);
---- or ------------------------
CString ?timestr ?=
"2000年04月05日"; ?int ?year,month,day;
BYTE ?tt[5];
//get ?year
memset(tt, ?0,
sizeof(tt)); ?tt[0] ?= ?timestr[0]; ?tt[1] ?= ?timestr[1]; ?tt[2] ?= ?timestr[2]; ?tt[3] ?= ?timestr[3]; ?year= ?atoi((char
*)tt); ?//get ?month
memset(tt, ?0,
sizeof(tt)); ?tt[0] ?= ?timestr[6]; ?tt[1] ?= ?timestr[7]; ?month ?= ?atoi((char ?*)tt); ?//get ?day
memset(tt, ?0,
sizeof(tt)); ?tt[0] ?= ?timestr[10]; ?tt[1] ?= ?timestr[11]; ?CTime ?time(year,month,day,0,0,0);
從上面來看,很明顯使用sscanf()函數的優勢.
二.將CTIme轉換為CString的方法:
CTime?tmSCan =
CTime::GetCurrentTime();
CString szTime = tmScan.Format("'%Y-%m-%d
%H:%M:%S'");
這樣得到的日期時間字符串就是以"2006-11-27
23:30:59"的格式.這是不是很方便呢?
//取得CTime中的日期
CString cstrDate =
tmScan.Format("%Y-%m-%d");
//取得CTime中的時間
CString cstrTime =
tmScan.Format("%H:%M-%S");
sprintf還有個不錯的表妹:strftime,專門用于格式化時間字符串的,用法跟她表哥很像,也是一大堆格式控制符,只是畢竟小姑娘家心細,她還要調用者指定緩沖區的最大長度,可能是為了在出現問題時可以推卸責任吧。這里舉個例子:
更多更好的sprintf()函數說明參考:《spirntf,你知道多少?》
time_t t = time(0);
//產生"YYYY-MM-DD
hh:mm:ss"格式的字符串。
char s[32];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S",
localtime(&t));
sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,這一對由于從面向對象哪里得到了贊助,用以寫出的代碼更覺優雅。
三,字符串轉換為數值類型
將字符串"20.0E6"轉換為數字
1,sscanf("20.0e5","%d",&x);
2,atof("20.0E6");
許多人用atoi(), atof() 和這個“家族”中的其它函數. 它們方便應用,但是有一個重要的缺點:
在轉換失敗和轉換字符串"0"時都返回0, 這樣使得一致性錯誤檢查變得幾乎不可能。 為了完整性我們給出了小段代碼:
代碼:
--------------------------------------------------------------------------------
const char* str_int =
"777";
const char* str_float =
"333.3";
int i = atoi(str_int);
float f =
atof(str_float);
--------------------------------------------------------------------------------
一個更好的辦法:
更有一點復雜, 更遺一致的辦法是利用sscanf()
代碼:
--------------------------------------------------------------------------------
const char* str_int =
"777";
const char* str_float =
"333.3";
int i;
float f;
if(EOF == sscanf(str_int,
"%d", &i)){
//錯誤
}
if(EOF == sscanf(str_float,
"%f", &f)){
//錯誤
}
--------------------------------------------------------------------------------
Since sscanf() takes a const char* parameter, you can directly
use a CString with it:
因為sscanf()用const char* 作為參數, 所以你可以直接用CString作參數:
代碼:
--------------------------------------------------------------------------------
CString str_int("777");
if(EOF == sscanf(str_int,
"%d", &i)){
//error
}
--------------------------------------------------------------------------------
小心格式描述符(如本例中的"%d")。
sscanf()沒有辦法檢查格式描述符與傳遞變量的類型匹配與否。如果不匹配你將得到不可預期的結果。
同樣注意sscanf()可以一次從字符串中提取一個或多個數值。 詳細信息請查閱MSDN。
C++ 方法
如下的例子展示了利用標準C++類的來完成這個任務的模板函數
代碼:
--------------------------------------------------------------------------------
#include
#include
#include
template
bool from_string(T &t,
const std::string &s,
std::ios_base &
(*f)(std::ios_base&))
{
std::istringstream
iss(s);
return
!(iss>>f>>t).fail();
}
int main()
{
int i;
float f;
//
from_string()的第三個參數應為如下中的一個
// one of std::hex, std::dec 或
std::oct
if(from_string(i,
std::string("ff"), std::hex)){
std::cout<
}
else{
std::cout<
failed"<<:endl>
}
if(from_string(f,
std::string("123.456"),
std::dec))
{
std::cout<
}
else{
std::cout<
failed"<<:endl>
}
return 0;
}
四,?int char * float and
CString?Covernt
1。 int
CString
1) int ->CString
int n = 1;
CString str;
str.Format("%d",n);
2) CString->int
CString str = "1";
int n = atoi(str.GetBuffer(0));
2. char* 與CString
1)char*->CString
char sz[128];
CString str;
str.Format("%s",sz);
2) CString -> char*
CString str;
//int nLength = str.GetLength();
char* sz = str.GetBuffer(0);
3.
floatCString
1)float->CString
float f = 0.0;
CString str;
str.Format("%f",f);
2) CString->float
CString str = "0.0";
float f = atof(str.GetBuffer(0));
總結
以上是生活随笔為你收集整理的c语言330转化成字符,c/字符串,字符转数字,数字转字符(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分期乐账单分期要审核多久 未通过可以这样
- 下一篇: 买单吧pay类支付是什么意思