Programming - cpueblo.com

GetStrToHex - char * 데이타를 Hex 값으로 구하기 ("0x86, 0xDD, 3a, 3b 9f 21" -> 0x86 0xDD 0x3A 0x3B 0x9F


글쓴이 : 유광희 날짜 : 2003-10-17 (금) 10:05 조회 : 15296

char *GetStrToHex(char *SrcStr, int *iLength)
{
	#define OutBufLength	10240
	char *SrcBuf = new char [strlen(SrcStr) + 1];
	char *OutBuf = new char [OutBufLength];
	char *t = NULL;
	int  Pos = 0;
	char *endptr;

	ZeroMemory(OutBuf, OutBufLength);
	memcpy(SrcBuf, SrcStr, strlen(SrcStr) + 1);

	t = strtok(SrcBuf, " ");
	while(1)
	{
		if (t == NULL)
			break;

		AnsiString StrTmp = t;
		StrTmp = StrTmp.Trim();

		OutBuf[Pos++] = strtol(StrTmp.c_str(), &endptr, 16);

		t = strtok(NULL, " ");

	}

	*iLength = Pos;
	return OutBuf;
}

void __fastcall TForm1::btnGetHexClick(TObject *Sender)
{
	String A = "0x86, 0xDD, 3a, 3b 9f 21";

	int Length = 0;
	char *p = GetStrToHex(A.c_str(), &Length);
}

// 결과.
// p =  { 0x86, 0xDD, 0x3a, 0x3B, 0x9F, 0x21 }
// length = 6