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

  • Home
  • SEARCH
  • 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 8325283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:22:45+00:00 2026-06-09T00:22:45+00:00

I know C++11 has some standard facilities which would allow to get integral values

  • 0

I know C++11 has some standard facilities which would allow to get integral values from unaligned memory. How could something like this be written in a more standard way?

template <class R>
inline R get_unaligned_le(const unsigned char p[], const std::size_t s) {
    R r = 0;
    for (std::size_t i = 0; i < s; i++)
        r |= (*p++ & 0xff) << (i * 8); // take the first 8-bits of the char
    return r;
}

To take the values stored in litte-endian order, you can then write:

uint_least16_t value1 = get_unaligned_le<uint_least16_t > (&buffer[0], 2);
uint_least32_t value2 = get_unaligned_le<uint_least32_t > (&buffer[2], 4);
  • 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-09T00:22:47+00:00Added an answer on June 9, 2026 at 12:22 am

    How did the integral values get into the unaligned memory to begin with?
    If they were memcpyed in, then you can use memcpy to get them out.
    If they were read from a file or the network, you have to know their
    format: how they were written to begin with. If they are four byte
    big-endian 2s complement (the usual network format), then something
    like:

    //      Supposes native int is at least 32 bytes...
    unsigned
    getNetworkInt( unsigned char const* buffer )
    {
        return buffer[0] << 24
            |  buffer[1] << 16
            |  buffer[2] <<  8
            |  buffer[3];
    }
    

    This will work for any unsigned type, provided the type you’re aiming
    for is at least as large as the type you input. For signed, it depends
    on just how portable you want to be. If all of your potential target
    machines are 2’s complement, and will have an integral type with the
    same size as your input type, then you can use exactly the same code as
    above. If your native machine is a 1’s complement 36 bit machine (e.g.
    a Unisys mainframe), and you’re reading signed network format integers
    (32 bit 2’s complement), you’ll need some additional logic.

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

Sidebar

Related Questions

I want to know if Netbeans has some option or setting that will allow
I would like to know how do I declare a record, that has some
I'm modifying a standard webcontrol in a system i'm working on which has some
Does anyone know a C library that has some standard probability functions like R
As you know TCL has some mathematical functions such as sin , cos ,
I'm new to HTML coding and I know HTML has some reserved characters for
I know this topic has been discussed but I think it has some differences.
Like you know, .NET Remoting has some limits, one of them is that server
I know this has been done many times before (some posts are really old
I need to know when a sharer has been authorized (to change some UI)

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.