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 6769237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:10:09+00:00 2026-05-26T15:10:09+00:00

I have a binary array char buffer[N]; which contains two bytes to be interpreted

  • 0

I have a binary array char buffer[N]; which contains two bytes to be interpreted as an unsigned short at its beginning and am currently extracting those by doing

unsigned short size= 0;
memcpy((char *) &size, buffer, sizeof(unsigned short));

I would however like to use std::copy for this. Is this possible?

Attempts like

std::copy(buffer, buffer+sizeof(unsigned short), (char *) &size);

have resulted in various errors when compiling..

Edit: Sorry, I was in a hurry and forgot:
This is on a Ubuntu GNU/Linux system with gcc 4.4.3. The error message was Error: Invalid conversion from ‘char*’ to ‘char’.

  • 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-05-26T15:10:09+00:00Added an answer on May 26, 2026 at 3:10 pm

    You should not use any sort of bulk copy routine for this, nor should you use typecasting to get a pointer to unsigned short, because none of those options take byte order into account. The correct way to extract a two-byte unsigned integer from a char[] buffer is with one of these functions:

    unsigned short extract_littleend16(const unsigned char *buf)
    {
        return (((unsigned short)buf[0]) << 0) |
               (((unsigned short)buf[1]) << 8);
    }
    
    unsigned short extract_bigend16(const unsigned char *buf)
    {
        return (((unsigned short)buf[0]) << 8) |
               (((unsigned short)buf[1]) << 0);
    }
    

    std::copy, memcpy, and direct pointer bashing will all do the same thing as one of these functions, but you don’t know which one it’ll be, and any time you have this task, one of these functions is right and the other one is wrong. Furthermore, if you don’t know which one of these you need from context, go up a couple design levels and figure it out.

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

Sidebar

Related Questions

I have a big lump of binary data in a char[] array which I
I have a char array which contains null characters at random locations. I tried
I have a binary data which I am reading into an array of long
I have a web service that returns the binary array of an object. Is
1-)For sorted array I have used Binary Search. We know that the worst case
I have a binary image in Matlab, and I need the binary array(0 and
I have an integer array: a=[3,4,5,6,7]; I want to convert it to a binary
Ok, so I'm reading a binary file into a char array I've allocated with
I have a binary array. In the process of conversion it into string,due to
I have a C++ class that writes its data out to a binary std::ofstream

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.