sswanglei 發表於 2007-2-9 16:08:00 /*------------------------------------------------------------
函數名 : TransData(CString InStr, PUCHAR OutStr)
功 能 : 將字符串轉換成十六進制數據
參 數 : CString InStr [in] 輸入字符串
PUCHAR OutStr [out] 輸出轉換後數據的指針
返回值 : BOOL,正確返回TRUE,錯誤返回FALSE
------------------------------------------------------------*/
BOOL TransData(CString InStr, PUCHAR OutStr)
{
UINT i = 0, j = 0;
UINT len = 0;
UCHAR Str[KEY_LEN * 2];
len = InStr.GetLength();
if ((len % 2) != 0)
{
MessageBox("請按要求輸入!", "錯誤", MB_OK | MB_ICONERROR);
return FALSE;
}
memset(OutStr, 0, sizeof(UCHAR[KEY_LEN]));
memset(Str, 0, sizeof(UCHAR[KEY_LEN * 2]));
strcpy((char*)(Str), InStr.GetBuffer(0));
InStr.ReleaseBuffer();
for(i = 0; i < len; i++)
{
if ((Str[i] > 47) && (Str[i] < 58))
{
Str[i] -= 48;
}
else if ((Str[i] > 64) && (Str[i] < 71))
{
Str[i] -= 55;
}
else if ((Str[i] > 96) && (Str[i] < 103))
{
Str[i] -= 87;
}
else
{
MessageBox("數據格式錯誤!", "錯誤", MB_OK | MB_ICONERROR);
return FALSE;
}
}
i = 0;
while(i < len)
{ // 轉換
Str[j] = Str[i] * 16 + Str[i + 1];
i += 2;
j++;
}