>> Hex to Decimal 변환 함수

설명 :
char 타입의 Hex문자를 unsigned Decimal 숫자형으로 변환

참조 :
#include <limits>
using namespace std;

함수정의 :
unsigned CharToXDigit(char c)
{
    if (c>='0' && c<='9') return static_cast<unsigned>(c-'0');
    else if (c>='A' && c<='F') return static_cast<unsigned>(c-'A')+0xA;
    else if (c>='a' && c<='f') return static_cast<unsigned>(c-'a')+0xa;
    else return std::numeric_limits<unsigned>::max(); // you can also return 0x10 if you prefer.
}


사용 예:
unsigned value;

value = CharToXDigit('A');
printf("value : 0x%02X, %d \n", value, value);


>>
value : 0x0A, 10

'Code Snippets > Function code' 카테고리의 다른 글

[C++] Hex to Decimal Convert (Multi characters)  (0) 2010.05.14
[C]Random Number Generator  (0) 2010.05.14
Posted by 혀나미
,