Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert binary data to an integral value

Question

What is the best way to convert binary to it's integral representation?

Context

Let's imagine that we have a buffer containing binary data obtained from an external source such as a socket connection or a binary file. The data is organised in a well defined format and we know that the first four octets represent a single unsigned 32 bit integer (which could be the size of following data). What would be the more efficient way to covert those octets to a usable format (such as std::uint32_t)?

Example

Here is what I have tried so far:

#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
#include <iostream>

int main()
{
    std::array<char, 4> buffer = { 0x01, 0x02, 0x03, 0x04 };
    std::uint32_t n = 0;

    n |= static_cast<std::uint32_t>(buffer[0]);
    n |= static_cast<std::uint32_t>(buffer[1]) << 8;
    n |= static_cast<std::uint32_t>(buffer[2]) << 16;
    n |= static_cast<std::uint32_t>(buffer[3]) << 24;
    std::cout << "Bit shifting:  " << n << "\n";

    n = 0;
    std::memcpy(&n, buffer.data(), buffer.size());
    std::cout << "std::memcpy(): " << n << "\n";

    n = 0;
    std::copy(buffer.begin(), buffer.end(), reinterpret_cast<char*>(&n));
    std::cout << "std::copy():   " << n << "\n";
}

On my system, the result of the following program is

Bit shifting:  67305985
std::memcpy(): 67305985
std::copy():   67305985
  1. Are they all standard compliant or are they using implementation defined behaviour?
  2. Which one is the more efficient?
  3. Is there an bette way to make that conversion?
like image 999
authchir Avatar asked Dec 04 '25 02:12

authchir


1 Answers

You essentially are asking about endianness. While your program might work on one computer, it might not on another. If the "well defined format" is network order, there are a standard set of macros/functions to convert to and from network order to the natural order for your specific machine.

like image 71
David Hammen Avatar answered Dec 06 '25 16:12

David Hammen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!