TEdit的一个问题
想把TEdit上的文本以16进制格式读入buffer,比如
ffff->\xff\xff
可当文本长度为32时,最后一个数总是读不进去
ffffffffffffffffffffffffffffffff->\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00
试过其它长度可以正常读取,,就是32的时候最后2字节总为00,该怎么解决呢
[解决办法]
你的代码效率太低,看我的:
int __stdcall CrnHexToByte(LPCSTR lpInBuf, LPBYTE lpOutBuf){ size_t uInLen; uInLen = strlen(lpInBuf); int nResult = 0; if (uInLen < 2) return nResult; BYTE c; for (size_t i = 0; i < uInLen; i += 2) { // 输出高4位 c = lpInBuf[i]; if (c >= '0' && c <= '9') lpOutBuf[nResult] = (c - '0') << 4; else if (c >= 'A' && c <= 'F') lpOutBuf[nResult] = (c - 'A' + 10) << 4; // 输出低4位 c = lpInBuf[i + 1]; if (c >= '0' && c <= '9') lpOutBuf[nResult++] |= (c - '0'); else if (c >= 'A' && c <= 'F') lpOutBuf[nResult++] |= (c - 'A' + 10); } return nResult;}void __fastcall TForm1::Button1Click(TObject *Sender){ BYTE buff[4096]; int n = CrnHexToByte(S1->Text.c_str(), buff); Caption = n;}