十进制转换十六进制字符比较

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> // 用于转换整数为十六进制字符串

// 定义结构体来存储 VID 和 PID
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();
}

// 定义函数,用于检查是否存在不支持的 VID 和 PID 组合
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;
}

// 定义函数,用于检查是否存在无响应的 USB 设备
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"}, // High Speed: Logitech USB Camera VID:046d, PID:08c9
        {"045e", "0719"}, // Full Speed: Xbox 360 ????? VID:045e, PID:071/
        {"045e", "0291"}, // Full Speed
        {"045e", "0084"}, // Low Speed: HID compliant mouse VID:045e, PID:0084
        {"03f0", "094a"}, // Low Speed
        {"093a", "2510"}, // Low Speed
        {"0b95", "1790"}, // Super Speed: ASIX AX88179 USB 3.0 to Gigabit Ethernet Adapter VID:0b95, PID:1790
        {"0bda", "8153"}, // Super Speed: ASIX AX88179 USB 3.0 to Gigabit Ethernet Adapter VID:0bda, PID:8153
        {"17ef", "6019"}, // High Speed
        {"0403", "6001"}, // Full Speed
        {"0b95", "772b"}, // Low Speed
        {"1a0a", "0201"}, // 6.7.23 VID=1a0a/PID=0201
        {"0930", "6544"}  // Add your comment here
    };
    std::vector<VidPid> noresponseVidPid = {
        {"1a0a", "0200"},
        {"090c", "1000"},
        {"0951", "1666"} // test
    };

    // 测试示例
    unVidPidSupported(unsupportedVidPid, 2385, 5734); // 期望返回 true
    noresponseUsb(noresponseVidPid, 2385,5734); // 期望返回 true

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泷fyk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值