QWORD HexStringToNumber( LPCSTR str ) { #define IsHexNumber( c ) ( ( c >= '0'&& c <= '9' ) || ( c >= 'A'&& c <= 'F' ) || ( c >= 'a'&& c <= 'f' ) ) #define GetHexNumber( c ) ( ( c >= '0'&& c <= '9' ) ? ( c - '0' ) : ( 10 + ( ( c >= 'A'&& c <= 'F' ) ? ( c - 'A' ) : ( c - 'a' ) ) ) ) if ( str == NULL ) return 0; QWORD result = 0; if ( str[0] == '0' && str[1] == 'x' ) str += 2; for ( int i = 0; i < 16; i ++ ) { if ( !IsHexNumber( str[i] ) ) break; result = ( result << 4 ) | GetHexNumber( str[i] ); } #undef GetHexNumber #undef IsHexNumber return result; }
CString TCCFrm::FrmGetFormatedColorFromEdit() { TCHAR szColor[7]; ::GetWindowText(m_hEditColor, szColor, 7); if (!IsHexNumber(szColor)) { ::_tcscpy_s(szColor, -1, _T("FFFFFF")); } int iLen = ::_tcslen(szColor); CString strColor(szColor); strColor.MakeUpper(); for (int i = 0; i < 6 - iLen; i ++) { strColor = _T("0") + strColor; } return strColor; }
bool IsHexNumber( const char* str, size_t len ) { // skip sign if there is one if( 1 >= len ) { if( '-' == str[0] || '+' == str[0] ) { str += 1; len -= 1; } } // skip "0x" or "0X" prefix if there is one if( 2 >= len ) { if( '0' == str[0] && ( 'x' == str[1] || 'X' == str[1] ) ) { str += 2; len -= 2; } } if( 0 == len ) return false; for(; len > 0; ++str, --len) { if( !IsHexNumber( *str ) ) return false; } return true; }
bool IsHexNumber( const std::string& str ) { return IsHexNumber( str.c_str(), str.length() ); }