Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:32:50+00:00 2026-06-02T07:32:50+00:00

Question What is the best way to convert binary to it’s integral representation? Context

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:32:52+00:00Added an answer on June 2, 2026 at 7:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

quick question, what's the best way to convert language a abbreviation to full name?
Similar to this question: Best way to convert Dictionary<string, string> into single aggregate String
I have a simple question. What is the best way to do a password
What is the best way to convert user input to UTF-8? I have a
I found another similar question: Best way to download images from XML content in
Question: What is the best way to set focus to a web control in
This question is related to this one: Best way to store product colors in
My question is: What is the best way to port the data back and
General question about java servlets and the best way to handle requests. If I
This is the best way I can think of phrasing this question, given this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.