- #include <ctype.h>
- #include <Windows.h>
- #include <Winbase.h>
- bool StringToHex(String^ in, array<Byte>^ txBuf)
- {
- bool c2rd = false;
- int cnt = 0;
- String^ s = gcnew String (in);
- s = s->Trim();
- s = s->ToUpper();
- for (auto i = 0; i<s->Length; i++) {
- if (isxdigit(s[i])) {
- Byte xdigi = isdigit(s[i])? s[i] - '0': s[i] - 'A';
- if (c2rd == true) {
- txBuf[cnt++] |= xdigi;
- c2rd = false;
- } else {
- txBuf[cnt] = (xdigi << 4);
- c2rd = true;
- }
- } else if (s[i] == ' ' || s[i] == 0x0D || s[i] == 0x0A) {
- //Just skip
- if (c2rd == true) { //Special case, single char
- txBuf[cnt++] >>= 4;
- c2rd = false;
- }
- } else {
- //Unkown format
- //c2rd = false;
- return false;;
- }
- }
- return true;
- }
- Compiled in Visual C++ 2010.
- //该片段来自于http://www.codesnippet.cn/detail/040920135622.html
来源: http://www.codesnippet.cn/detail/040920135622.html