gpt4 book ai didi

c++ - 从 float 组创建缓冲区

转载 作者:行者123 更新时间:2023-11-28 04:49:41 27 4
gpt4 key购买 nike

我想使用 Boost 库通过网络发送一组 float 。为此,我必须从该数组创建一个缓冲区,但它似乎不起作用。

在这个例子中http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/tutorial/tutdaytime4/src.html它是用一个字符数组完成的,但我无法用一个 float 组来复制。

我尝试使用这个构造函数 http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/buffer/overload6.html但我还是无法达到我的目标。

boost::array<float, 512> arr = { { 0.0f } };
auto buffer = boost::asio::buffer(arr, arr.size());

现在,我想从缓冲区中找到 0.0f。我尝试使用 static_cast 但它引发了错误。

最佳答案

缓冲区本质上是八位字节序列。

你做的地方有很大的错误

auto buffer = boost::asio::buffer(arr, arr.size());

因为那里,arr.size() 以字节 为单位(不是float 元素的数量)。修复它的最佳方法是让 Boost 正确计算大小:

auto buffer = boost::asio::buffer(arr); // fixed

现在剩下的,从缓冲区读回 float 没有多大意义(你还有数组,所以为什么不使用它呢?)。但是如果你必须,你可以使用buffer_cast:

// extract the first float back - pretend we don't know it's actually the `sending` array
std::cout << "Pi is " << boost::asio::buffer_cast<float const*>(buffer)[0] << "\n";

演示时间

让我们做一个演示

  • 将缓冲区写入流
  • 流的十六进制转储(以显示实际的八位字节0
  • 往返它(读回另一个 float 组)
  • 验证结果

Live On Coliru

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <sstream>
#include <iostream>
#include <iomanip>

using Floats = boost::array<float, 10>;

int main() {

// use a stream to "mock" a communication channel
std::stringstream ss;

// fill the stream
{
Floats sending = { { M_PI } };
auto buffer = boost::asio::buffer(sending);
std::copy(buffers_begin(buffer), buffers_end(buffer), std::ostreambuf_iterator<char>(ss));

// extract the first float back - pretend we don't know it's actually the `sending` array
std::cout << "Pi is " << boost::asio::buffer_cast<float const*>(buffer)[0] << "\n";
}

// for debug only, print the octects representing the stream contents
{
auto n = 0;
for (uint8_t ch : ss.str()) {
std::cout << std::hex << "0x" << std::setw(2) << std::setfill('0') << static_cast<int>(ch) << ((n++%4==3)?"\n":" ");
}
}

// now let's roundtrip that float buffer!
{
Floats roundtrip = { { M_PI } };
auto buffer = boost::asio::buffer(roundtrip);
std::copy(std::istreambuf_iterator<char>(ss), {}, buffers_begin(buffer));

// now, you can - of course use the buffer_cast again
std::cout << "Pi is " << boost::asio::buffer_cast<float const*>(buffer)[0] << "\n";

// but it makes a lot more sense to use the underlying array directly:
std::cout << "Directly from the roundtripped array: " << roundtrip[0] << "\n";
}
}

打印

Pi is 3.14159
0xdb 0x0f 0x49 0x40
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00
Pi is 3.14159
Directly from the roundtripped array: 3.14159

关于c++ - 从 float 组创建缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48491887/

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