將CString字符串輸入轉(zhuǎn)化成整數(shù)的實現(xiàn)方法
更新時間:2016年09月25日 20:33:34 投稿:jingxian
下面小編就為大家?guī)硪黄獙String字符串輸入轉(zhuǎn)化成整數(shù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
如下所示:
BOOL IsHexFormat(LPCTSTR pStr) { if (pStr[0] == L'0' && ((pStr[1] == L'x') || (pStr[1] == L'X'))){ return TRUE; } return FALSE; } BOOL IsInputValid(LPCTSTR pStr) { int i; BOOL res; BOOL IsHex; i = 0; res = TRUE; IsHex = IsHexFormat(pStr); while (pStr[i] != L'\0'){ if (pStr[i] >= L'0' && pStr[i] <= L'9'){ i++; continue; } else if (IsHex && (i == 1)){ i++; continue; } else if (IsHex && ((pStr[i] >= L'a' && pStr[i] <= L'f') || (pStr[i] >= L'A' && pStr[i] <= L'F') )) { i++; continue; } else{ res = FALSE; break; } } return res; } UINT32 CStrHex2Uint32(LPCTSTR pStr) { int i = 0; UINT32 res = 0; while (pStr[i] != L'\0'){ if (pStr[i] >= L'0' && pStr[i] <= L'9'){ res = res * 16 + pStr[i] - L'0'; } else if (pStr[i] >= L'a' && pStr[i] <= L'f'){ res = res * 16 + pStr[i] - L'a' + 10; } else if (pStr[i] >= L'A' && pStr[i] <= L'F'){ res = res * 16 + pStr[i] - L'A' + 10; } else{ break; } i++; } return res; } /* 將CString轉(zhuǎn)化成UINT32, 0x開頭的識別成十六進(jìn)制,其它為十進(jìn)制*/ BOOL CStr2Uint32(CString str, UINT32 *pData) { LPCTSTR pStr; pStr = (LPCTSTR)str; if (!IsInputValid(pStr)){ *pData = 0; return FALSE; } if (IsHexFormat(pStr)){ UINT32 Data; pStr = &pStr[2]; *pData = CStrHex2Uint32(pStr); } else{ *pData = _wtoi((wchar_t *)pStr); } return TRUE; }
以上就是小編為大家?guī)淼膶String字符串輸入轉(zhuǎn)化成整數(shù)的實現(xiàn)方法的全部內(nèi)容了,希望對大家有所幫助,多多支持腳本之家~
相關(guān)文章
簡單分析C語言中指針數(shù)組與數(shù)組指針的區(qū)別
這篇文章主要介紹了C語言中指針數(shù)組與數(shù)組指針的區(qū)別,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-11-11淺談帶緩沖I/O 和不帶緩沖I/O的區(qū)別與聯(lián)系
下面小編就為大家?guī)硪黄獪\談帶緩沖I/O 和不帶緩沖I/O的區(qū)別與聯(lián)系。小編覺得挺不錯的現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01c++創(chuàng)建二維動態(tài)數(shù)組與內(nèi)存釋放問題
這篇文章主要介紹了c++創(chuàng)建二維動態(tài)數(shù)組與內(nèi)存釋放問題,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06