// // Created by robin on 6/29/15. // #include #include #include "libpcsc_cpptools/ScHex.h" const char ScHex::hexCharToInt(const char& c) { if (c >= 'a') return (char)(10 + (c - ('a' - 'A')) - 'A'); if (c >= 'A') return (char)(10 + c - 'A'); return c - '0'; } const std::string ScHex::stringToByteArray(const std::string &str) { std::string hexa; for (auto c : str) { if (isxdigit(c)) hexa += c; } if (hexa.size() % 2) hexa.insert(0, "0"); auto data = new BYTE[hexa.size() / 2]; for (size_t i = 0; i < hexa.size(); i += 2) { data[i / 2] = (hexCharToInt(hexa[i]) << 4) | hexCharToInt(hexa[i + 1]); } return std::string((char*)data, hexa.size() / 2); } const std::string ScHex::byteArrayToString(const std::string &bytes, const std::string &separator) { std::string res; for (DWORD i = 0; i < bytes.size();++i) { res += intToHexChar(bytes.data()[i]); if (i != bytes.size() - 1) res += separator; } return res; } const std::string ScHex::intToHexChar(const char &c) { std::string res; for (auto c1 : {(c & 0xf0) >> 4, c & 0x0f}) { if (c1 < 10) res += '0' + c1; else res += 'A' + (c1 - 10); } return res; }