从浮点数组创建缓冲区
Create a buffer from an array of floating points
我想使用 Boost 库通过网络发送一组浮点数。为此,我必须从该数组创建一个缓冲区,但它似乎不起作用。
在这个例子中http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/tutorial/tutdaytime4/src.html它是用一个字符数组完成的,但我不能用一个浮点数组来复制。
我尝试使用此构造函数 http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/buffer/overload6.html 但我还是无法访问我的 objective。
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
现在剩下的,从缓冲区读回浮点数没有多大意义(你还有数组,为什么不使用它呢?)。但是如果你必须,你可以使用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
- 往返它(读回另一个浮点数组)
- 验证结果
#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
我想使用 Boost 库通过网络发送一组浮点数。为此,我必须从该数组创建一个缓冲区,但它似乎不起作用。
在这个例子中http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/tutorial/tutdaytime4/src.html它是用一个字符数组完成的,但我不能用一个浮点数组来复制。
我尝试使用此构造函数 http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/buffer/overload6.html 但我还是无法访问我的 objective。
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
现在剩下的,从缓冲区读回浮点数没有多大意义(你还有数组,为什么不使用它呢?)。但是如果你必须,你可以使用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
- 往返它(读回另一个浮点数组)
- 验证结果
#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