Retrieving Bytes data from boost::asio: array

Viewed 22

I am trying to retrieve data from boost array with Bytes data which should at the end be 2 double. I don't why but I can't make the conversion of the buffer to a readable format here is my attempt :

boost::array<void*, 1024> arr;
boost::system::error_code error_2;
socket_2.read_some(boost::asio::buffer(arr), error_2);  
for (int k = 0; k < 32; k++) { std::cout << arr[k]; }

I don't understand how to retrieve the data from arr, memcpy is not accepted for instance.

1 Answers

Effectively, whatever you do will amount to memcpy. But you can make things more elegant:

boost::array<double, 4096 / sizeof(double)> arr;

boost::system::error_code ec;

size_t n = s.read_some(asio::buffer(arr), ec);

for (size_t i = 0; i < (n / sizeof(double)); ++i) {
    std::cout << arr[i];
}

If you already have a raw buffer, consider asio::buffer_cast<>

Related