아스키 값을 Hex 값으로 바꾸는 함수
페이지 정보
본문
아스키 값을 Hex로 바꾸는 함수가 어떤 것인지 궁금합니다.
Hex 문자열을 정수값으로 변환하는 프로그램을 말하는 것 같은데 다음의 루틴을 이용해 보기 바랍니다. 런타임 라이브러리에는 그런 함수가 없는 걸로 알고 있습니다.
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#undef
//
UINT StrHexToInt(LPCTSTR str)
{
return (UINT)StrHexToLong(str);
}
ULONG StrHexToLong(LPCTSTR str)
{
CString work;
int nLength;
ULONG ulRetVal;
work = str;
if(strstr(str, "0x") != NULL)
work = strstr(str, "0x") + 2;
//
nLength = work.GetLength() - 1;
ulRetVal = 0;
work.MakeUpper();
for(int i=0; i<work.GetLength(); i++, nLength--) {
switch(work.GetAt(i)) {
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
ulRetVal += (work.GetAt(i) - '0') * (int)pow(16, nLength);
break;
case 'A' :
case 'B' :
case 'C' :
case 'D' :
case 'E' :
case 'F' :
ulRetVal += (10 + work.GetAt(i) - 'A') * (int)pow(16, nLength);
break;
default :
return ulRetVal = 0;
}
}
return ulRetVal;
}
십진수로 변환되면 다른 진수로도 충분히 바꿀 수 있을 것입니다.
Hex 문자열을 정수값으로 변환하는 프로그램을 말하는 것 같은데 다음의 루틴을 이용해 보기 바랍니다. 런타임 라이브러리에는 그런 함수가 없는 걸로 알고 있습니다.
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#undef
//
UINT StrHexToInt(LPCTSTR str)
{
return (UINT)StrHexToLong(str);
}
ULONG StrHexToLong(LPCTSTR str)
{
CString work;
int nLength;
ULONG ulRetVal;
work = str;
if(strstr(str, "0x") != NULL)
work = strstr(str, "0x") + 2;
//
nLength = work.GetLength() - 1;
ulRetVal = 0;
work.MakeUpper();
for(int i=0; i<work.GetLength(); i++, nLength--) {
switch(work.GetAt(i)) {
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
ulRetVal += (work.GetAt(i) - '0') * (int)pow(16, nLength);
break;
case 'A' :
case 'B' :
case 'C' :
case 'D' :
case 'E' :
case 'F' :
ulRetVal += (10 + work.GetAt(i) - 'A') * (int)pow(16, nLength);
break;
default :
return ulRetVal = 0;
}
}
return ulRetVal;
}
십진수로 변환되면 다른 진수로도 충분히 바꿀 수 있을 것입니다.
댓글목록
등록된 댓글이 없습니다.