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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:29:23+00:00 2026-06-07T16:29:23+00:00

I need to do the following things very often (endianness can be ignored here,

  • 0

I need to do the following things very often (endianness can be ignored here, I only use x86):

  1. compare a given uint to a specific offset in a byte array (maybe I can compare all 4 bytes at once?)
  2. copy 4 bytes from a byte array with offset to a uint
  3. copy a uint to a specific offset in a byte array (it will overwrite 4 bytes in this array)
  4. copy 12 bytes to a struct: (uint, uint, byte, byte,byte, byte)

The last one is not so often used. But it would be very interesting if I could do it with just some unsafe operations.

What is the fastest way to do it? The first is the most important as I do it the most and is uses the most CPU time. Unsafe code is possible (if it is faster).

EDIT:

Currenlty I am using something like this to copy an uint into a byte array:

 public static void Copy(uint i, byte[] arr, int offset)
    {
        var v = BitConverter.GetBytes(i);
        arr[offset] = v[0];
        arr[offset + 1] = v[0 + 1];
        arr[offset + 2] = v[0 + 2];
        arr[offset + 3] = v[0 + 3];
    }

For the back conversion I use this:

BitConverter.ToUInt32(arr, offset)

The last one is so less code, the first one so much, maybe there is optimization.
For comparison, currently I convert it back (the second one) and then I compare the uint with the value I want to compare:

BitConverter.ToUInt32(arr, offset) == myVal

For the fourth Part (extracting a struct) I am using something like this:

    [StructLayout(LayoutKind.Explicit, Size = 12)]
    public struct Bucket
    {
        [FieldOffset(0)]
        public uint int1;
        [FieldOffset(4)]
        public uint int2;
        [FieldOffset(8)]
        public byte byte1;
        [FieldOffset(9)]
        public byte byte2;
        [FieldOffset(10)]
        public byte byte3;
        [FieldOffset(11)]
        public byte byte4;
    }

    public static Bucket ExtractValuesToStruct(byte[] arr, int offset)
    {
        var b = new Bucket();
        b.int1 = BitConverter.ToUInt32(arr, offset);
        b.int2 = BitConverter.ToUInt32(arr, offset + 4);
        b.byte1 = arr[offset + 8];
        b.byte2 = arr[offset + 9];
        b.byte3 = arr[offset + 10];
        b.byte4 = arr[offset + 11];
        return b;
    }

I think using unsafe code I should be able to copy the 12 bytes at once.

  • 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-07T16:29:24+00:00Added an answer on June 7, 2026 at 4:29 pm

    Copy a uint to a specific offset in a byte array

      unsafe public static void UnsafeCopy(uint i, byte[] arr, int offset)
      {
        fixed (byte* p = arr)
        {
          *((uint*)(p + offset)) = i;
        }
      }
      public static void ShiftCopy(uint i, byte[] arr, int offset)
      {
        arr[offset] = (byte)(i & 0xFF);
        arr[offset + 1] = (byte)((i >> 8) & 0xFF);
        arr[offset + 2] = (byte)((i >> 16) & 0xFF);
        arr[offset + 3] = (byte)((i >> 24) & 0xFF);
      } 
    

    Statistic (1 000 000 calls)

    00:00:00.0414024: copy. get bytes
    00:00:00.0136741: unsafe copy
    00:00:00.0154764: shift  copy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do the following things for a text file..and insert the seeked
I need following namespaces to use native wpf property grid however, VS 2010 doesn't
I have a hash very similar to the following JSON object; This is only
I need to do the following thing: var a = from c in DB.Customers
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need the following C function in Python: int func(Uint8 *bytRecvBuffer, int *iRecvLen); I
I need the following xml to be made in code: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:gravity=right
I need the following authentication script finished. I am weak at php/pdo so I
I need the following logic. If array contains value , return it else return
I really need the following in magento. When a customer shows the config product

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.