将整数向量转换为字节数组向量,然后调用每个字节数组
Converting an integer vector into a vector of byte arrays then calling each byte array
我有一个数字向量,例如。 vector<int> vec={16383 1 8192}
.我需要将它们转换为字节数组,以便 SPI 写入函数可以读取它并传输数据。但是,我无法获得字节数组向量的工作。我是C++新手。
基本上,如果我的代码不清楚(可能是(,我想从
std::vector <int> output={16383,1,8192};
自
std::vector <byte array> finalbuffer= {{0,63,255},{0,0,1},{0,32,0}};
所以我可以打电话给finalbuffer[0]
,它会包含{0,63,255}
.
我尝试查找此问题,但答案一直悬而未决,并不完全是我需要的。我尝试制作一个多维数组,但是我的初始向量大小并不总是相同的,并且取决于读取的数据。
std::vector <int> output={16383,1,8192};
//the basic code to convert from integer to byte array, I don't know how
// to convert the int vector to a byte array vector
uint8 tx_buffer1[3] = { 0,0,0 };
for (unsigned i = 0; i < output.size(); ++i) {
for (unsigned j = 0; j < 3; ++j) {
tx_buffer1[j] = (output[i] >> (2 - j) * 8) & 0xFF;
cout << unsigned(tx_buffer1[j]) << endl;
}
}
//The above works such that
int output=16383;
uint8 tx_buffer1[3] = { 0,0,0 };
for (unsigned j = 0; j < 3; ++j) {
tx_buffer1[j] = (output >> (2 - j) * 8) & 0xFF;
cout << unsigned(tx_buffer1[j]) << endl;
}
tx_buffer[0]=0
tx_buffer[1]=63
tx_buffer[2]=255
// But I need it for multiple integers from the vector.
以下内容适用于uint8 finalbuffer= {0-255,0-255,0-255}
. handle
、&transfercount
和options
都不重要。3 表示它读取字节,我需要它,以便它读取每个元素中带有字节数组的向量
std::vector <byte array> finalbuffer= {{0,63,255},{0,0,1},{0,32,0}};
//finalbuffer[0] would contain {0,63,255}
//finalbuffer[1] would contain {0,0,1}
//...
for {unsigned i=0; i<finalbuffer.size(); ++i) {
status = SPI_Write(handle, finalbuffer[i], 3, &transferCount, options);
if (status != FT_OK)
print_and_quit("Error while transmitting bytes.");
}
我无法让我的代码工作。不是字节数组向量的更好方法?
,我不知何故明白了,但不完全明白。也许我的答案因此不准确。
但首先,您要将带有整数的向量转换为具有 3 个字节的向量。
对于具有 3 个字节的东西,我们将使用大小为 3 的 std::字节数组。我们将对所有内容使用未签名。
然后我们创建这个数组的向量。
无符号整数的整个向量可以在一个转换语句中转换为字节数组向量。这很容易。我们也可以将其复制到一个普通的旧 C 样式数组中。
但这不是必需的。您可以使用std::array
的数据功能访问基础数据。
这是转换代码
#include <vector>
#include <array>
#include <cstdint>
#include <algorithm>
#include <iterator>
#include <iostream>
using Byte = uint8_t;
using B3Array = std::array<Byte, 3>;
std::vector <unsigned int> output = { 16383,1,8192 };
std::vector<B3Array> finalbuffer{};
Byte plainCStyleArray[3];
int main()
{
// Convert to byte array, C++ style
std::transform( output.begin(), output.end(),std::back_inserter(finalbuffer),
[](const unsigned int i) {
Byte b0 = (i >> 16) & 0xFF; Byte b1 = (i >> 8) & 0xFF; Byte b2 = (i) & 0xFF;
B3Array ba{ b0,b1,b2 };
return ba;
}
);
// Copy one vector to plain C-Style array (but why?)
std::copy_n(finalbuffer[0].begin(), 3, plainCStyleArray);
// Play a little bit. Some test code
for (size_t i = 0; i < output.size(); ++i) {
B3Array b3a = finalbuffer[i];
std::copy_n(b3a.begin(), 3, std::ostream_iterator<int>(std::cout, " "));
std::cout << 'n';
}
return 0;
}
然后你可以在你的函数中写(在std::transform
之后(:
for {unsigned i=0; i<finalbuffer.size(); ++i) {
status = SPI_Write(handle, finalbuffer[i].data(), 3, &transferCount, options);
if (status != FT_OK)
print_and_quit("Error while transmitting bytes.");
}
如您所见,我刚刚在finalbuffer[i]
中添加了.data()
这一切看起来都很简单,我想,我可能误解了你的问题。对不起。。。
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- C++如何将字符数组转换为字节向量(向量<byte>)
- 将对象的字节复制到数组并再次复制回来是否安全
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- C++字节数组到整数
- 将字符串转换为无符号字符数组/字节数组
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- 从 UTF-8 字节数组创建字符串?
- 如何将 CString 转换为字节 [] 数组?
- 使用SSE内部函数将布尔数组(8字节布尔)转换为int或char