C++ 进制转换
核心代码
std::string intToHexString(int value) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(4) << std::hex << value;
return ss.str();
}
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
struct VidPid {
std::string vid;
std::string pid;
};
std::string intToHexString(int value) {
std::stringstream ss;
ss << std::setfill('0') << std::setw(4) << std::hex << value;
return ss.str();
}
bool unVidPidSupported(const std::vector<VidPid>& unsupportedVidPid, int vid, int pid) {
std::string vidStr = intToHexString(vid);
std::string pidStr = intToHexString(pid);
for (const auto& vidpid : unsupportedVidPid) {
if (vidpid.vid == vidStr && vidpid.pid == pidStr) {
return true;
}
}
return false;
}
bool noresponseUsb(const std::vector<VidPid>& noresponseVidPid, int vid, int pid) {
std::string vidStr = intToHexString(vid);
std::string pidStr = intToHexString(pid);
for (const auto& vidpid : noresponseVidPid) {
if (vidpid.vid == vidStr && vidpid.pid == pidStr) {
std::cout << "true noresponseUsb fyk" << std::endl;
return true;
}
}
std::cout << "false noresponseUsb fyk" << std::endl;
return false;
}
int main() {
std::vector<VidPid> unsupportedVidPid = {
{"046d", "08c9"},
{"045e", "0719"},
{"045e", "0291"},
{"045e", "0084"},
{"03f0", "094a"},
{"093a", "2510"},
{"0b95", "1790"},
{"0bda", "8153"},
{"17ef", "6019"},
{"0403", "6001"},
{"0b95", "772b"},
{"1a0a", "0201"},
{"0930", "6544"}
};
std::vector<VidPid> noresponseVidPid = {
{"1a0a", "0200"},
{"090c", "1000"},
{"0951", "1666"}
};
unVidPidSupported(unsupportedVidPid, 2385, 5734);
noresponseUsb(noresponseVidPid, 2385,5734);
return 0;
}