int CCommunicateCommDlg::Str2Hex(PBYTE pOut, int nOutLen, PBYTE pIn, int nInLen){ int nLen = (nInLen + 1) / 3;
if (nOutLen < nLen) {
AfxMessageBox(_T("输出Buff容量不够..."));
return - 1;
}
CharUpperA(LPSTR(pIn));
int i = 0;
for (i = 0; i < nInLen; ++i) {
if (! ((pIn[i] >= '0' && pIn[i] <= '9') || (pIn[i] >= 'A' && pIn[i] <= 'F') || ' ' == pIn[i])) {
AfxMessageBox(_T("输入的待加密数据错误,非十六进制格式..."));
return - 1;
}
}
memset(pOut, 0, nOutLen);
for (i = 0; i < nLen; ++i) {
BYTE Hi4 = (pIn[i * 3] <= '9') ? (pIn[i * 3] - '0') : (pIn[i * 3] - 'A' + 10);
BYTE Lo4 = (pIn[i * 3 + 1] <= '9') ? (pIn[i * 3 + 1] - '0') : (pIn[i * 3 + 1] - 'A' + 10);
pOut[i] = BYTE(Hi4 << 4) + Lo4;
}
return i;
}