string到hex的转换(部分截取)(字符16进制到数值16进制)

来源:互联网 发布:sublimetext mac 破解 编辑:程序博客网 时间:2024/06/03 18:30
以下:
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;}

1 0
原创粉丝点击