将CString字符串输入转化成整数的实现方法

2019/7/10 22:54:35

本文主要是介绍将CString字符串输入转化成整数的实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

如下所示:

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转化成UINT32, 0x开头的识别成十六进制,其它为十进制*/ 
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; 
} 

以上就是小编为大家带来的将CString字符串输入转化成整数的实现方法的全部内容了,希望对大家有所帮助,多多支持找一找教程网~



这篇关于将CString字符串输入转化成整数的实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程