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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:50:00+00:00 2026-05-29T07:50:00+00:00

I have a byte array containing 16 & 32bit data samples, and to cast

  • 0

I have a byte array containing 16 & 32bit data samples, and to cast them to Int16 and Int32 I currently just do a memcpy with 2 (or 4) bytes.

Because memcpy is probably isn’t optimized for lenghts of just two bytes, I was wondering if it would be more efficient to convert the bytes using integer arithmetic (or an union) to an Int32.

I would like to know what the effiency of calling memcpy vs bit shifting is, because the code runs on an embedded platform.

  • 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-29T07:50:01+00:00Added an answer on May 29, 2026 at 7:50 am

    I would say that memcpy is not the way to do this. However, finding the best way depends heavily on how your data is stored in memory.

    To start with, you don’t want to take the address of your destination variable. If it is a local variable, you will force it to the stack rather than giving the compiler the option to place it in a processor register. This alone could be very expensive.

    The most general solution is to read the data byte by byte and arithmetically combine the result. For example:

    uint16_t res = (  (((uint16_t)char_array[high]) << 8)
                    | char_array[low]);
    

    The expression in the 32 bit case is a bit more complex, as you have more alternatives. You might want to check the assembler output which is best.

    Alt 1: Build paris, and combine them:

    uint16_t low16 = ... as example above ...;
    uint16_t high16 = ... as example above ...;
    uint32_t res = (  (((uint32_t)high16) << 16)
                    | low16);
    

    Alt 2: Shift in 8 bits at a time:

    uint32_t res = char_array[i0];
    res = (res << 8) | char_array[i1];
    res = (res << 8) | char_array[i2];
    res = (res << 8) | char_array[i3];
    

    All examples above are neutral to the endianess of the processor used, as the index values decide which part to read.

    Next kind of solutions is possible if 1) the endianess (byte order) of the device match the order in which the bytes are stored in the array, and 2) the array is known to be placed on an aligned memory address. The latter case depends on the machine, but you are safe if the char array representing a 16 bit array starts on an even address and in the 32 bit case it should start on an address dividable by four. In this case you could simply read the address, after some pointer tricks:

    uint16_t res = *(uint16_t *)&char_array[xxx];
    

    Where xxx is the array index corresponding to the first byte in memory. Note that this might not be the same as the index to he lowest value.

    I would strongly suggest the first class of solutions, as it is endianess-neutral.

    Anyway, both of them are way faster than your memcpy solution.

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

Sidebar

Related Questions

I have a byte[] containing data for a file. Array can contain data for
I have a byte array containing an MP3 stream. Is it correct to assume
I have a byte array containing bytes from a file( see my last question
I have a byte offset for a byte array containing a UTF-8 encoded string,
Hi i am making application in c#.I have byte array of containing hex values.I
I have a c# byte[] containing sensitive data. What is the best way to
I am making C# windows application.In that application i have one byte array containing
I have a large int array containing image data in the format ARGB (alpha,
I have an array returned from a web service containing byte values (non binary
I have a byte array (say, UInt8 *somebuffer) with data in an unknown format.

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.