gpt4 book ai didi

c++ - 字符串到 ascii 十六进制/整数值

转载 作者:行者123 更新时间:2023-11-28 04:18:50 25 4
gpt4 key购买 nike

<分区>

我正在尝试将字符串转换为 ASCII 值 uint8_t 的 vector

例如,如果我有一个 "500" 的字符串,我想要一个 {53, 48, 48} 的 vector ,其中这些值为十六进制 0x35 , 0x30, 0x30。这是我目前正在做的,但没有用

#include <iostream>
#include <string>
#include <vector>
int main() {
std::string number = "500";
std::vector<std::uint8_t> bytes;
for (auto& c : number)
bytes.push_back(static_cast<std::uint8_t>(c));

for (auto& c : bytes)
std::cout << c << std::endl;
return 0;
}

但我只得到 5、0、0 作为输出,而我期望的是 53、48、48

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com