C++怎么用std::cout输出0x63、0x69
时间: 2023-05-22 08:04:11 浏览: 70
可以通过以下代码实现:
```
#include <iostream>
using namespace std;
int main() {
cout << hex << showbase << 0x63 << endl;
cout << hex << showbase << 0x69 << endl;
return 0;
}
```
输出结果为:
```
0x63
0x69
```
相关问题
树莓派server.cpp:(.text+0x28): undefined reference to `std::cout'
树莓派server.cpp文件出现了undefined reference to `std::cout'的错误,这个错误通常是由于编译器找不到对应的库文件引起的。std::cout是C++标准库中的输出流对象,因此需要在编译时链接C++标准库。可以在编译命令中添加-lstdc++选项来链接C++标准库,例如:
g++ server.cpp -o server -lstdc++
这样就可以解决undefined reference to `std::cout'的错误了。
c++ std::vector<uint8_t> AA = {0x11, 0x22, 0x33,};不使用std::stringstream的方式,将AA内容输出一个是字符串“0x11,0x22,0x33”
可以使用循环遍历 `AA` 中的元素,将其转换成字符串后拼接起来,最后输出即可。以下是示例代码:
```cpp
#include <iostream>
#include <vector>
#include <cstdint>
#include <string>
int main() {
std::vector<uint8_t> AA = {0x11, 0x22, 0x33};
std::string s;
for (auto it = AA.begin(); it != AA.end(); ++it) {
char buf[5];
sprintf(buf, "0x%02x", static_cast<int>(*it));
s += buf;
if (it != AA.end() - 1) {
s += ",";
}
}
std::cout << s << std::endl; // 输出:0x11,0x22,0x33
return 0;
}
```
代码中使用了 `sprintf` 函数来将数字转换成字符串,`"0x%02x"` 是指定输出的数字以十六进制形式显示,且宽度为2位,不足的前面补0。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)