在c++中打印字节流
Printing out byte stream in C++
在这个网站的帮助下,c++的int到字节数组,我有一个代码来序列化int到字节流。
来自整数值1234的字节流数据是大端格式的'x00x00x04xd2',我需要提出一个实用程序函数来显示字节流。这是我的第一个版本
#include <iostream>
#include <vector>
using namespace std;
std::vector<unsigned char> intToBytes(int value)
{
std::vector<unsigned char> result;
result.push_back(value >> 24);
result.push_back(value >> 16);
result.push_back(value >> 8);
result.push_back(value );
return result;
}
void print(const std::vector<unsigned char> input)
{
for (auto val : input)
cout << val; // <--
}
int main(int argc, char *argv[]) {
std::vector<unsigned char> st(intToBytes(1234));
print(st);
}
我如何在屏幕上得到正确的值在十进制和十六进制?
For hex:
for (auto val : input) printf("\x%.2x", val);
为十进制:
for (auto val : input) printf("%d ", val);
相关文章:
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 从 C++ 中的无符号字符字节流中读取值
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- C++ 中的字节流
- 错误:变量或字段'PrintEntity'声明无效无效打印实体(实体 e);
- Microsoft加密 API 是否允许从字节流创建 ECDSA 密钥?
- g++ 命令行宏定义字节流
- 使用媒体基础从h.264字节流(附件B)创建MP4容器
- 访问程序中的符号表以打印结构的字段名称
- 如何从TCP字节流中提取消息的长度信息,并仅提取这么多消息
- 双值传输在字节流中
- 将 tiff 图像转换为字符串,以作为二进制应用程序/八位字节流 (C++) 发布
- 如何使用 PJSIP 库解码 SIP 字节流
- 在c++中将字节流拆分为位
- 在c++中打印字节流
- c++ Windows JPEG到字节流
- 流迭代器——字节流
- 为什么我通过串行发送的字节流(写入/dev/tty*)会重新映射回来
- Array2d从字节流不写入文件到磁盘
- 在Java程序中通过UDP接收c++字节流