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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:49:34+00:00 2026-06-16T09:49:34+00:00

I am working on a C# WinForms application that reads/writes data to/from a hardware

  • 0

I am working on a C# WinForms application that reads/writes data to/from a hardware device. My application has a multiselect listbox which contains the numbers 1 – 100000 and the user may select up to 10 numbers. When they’re done selecting each number, the user clicks a button and my event handler code needs to build a fixed-size (30 bytes) byte array using 3 bytes to represent each selected number and pad the array if less than 10 numbers were selected.

As an example, suppose my user chooses the following values:

17
99152
3064
52588
65536

I’m currently using this code to convert each number into a byte array:

byte[] bytes = BitConverter.GetBytes(selectedNumber);
Array.Reverse(bytes) // because BitConverter.IsLittleEndian() = true
Debug.WriteLine(BitConverter.ToString(bytes));

For the numbers I listed above, this produces the following:

00-00-00-11
00-01-83-50
00-00-0B-F8
00-00-CD-6C
00-01-00-00

BitConverter is giving me back a 4 byte array where I only have space to use 3 bytes to store each number in the final byte array. I can drop the most significant byte of each individual byte array and then build my final array like this:

00-00-11-01-83-50-00-0B-F8-00-CD-6C-01-00-00-[padding here]

Writing that to the device should work. But reading the array (or a similar array) back from the device causes a bit of a problem for me. When I have a 3 byte array and try to convert that into an int using this code…

int i = BitConverter.ToInt32(bytes, 0);

…I get “Destination array is not long enough to copy all the items in the collection.” I suppose I could insert a most significant byte of 0x00 at the beginning of every three bytes and then convert that but is there a better way to do this?

  • 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-16T09:49:36+00:00Added an answer on June 16, 2026 at 9:49 am

    I would imagine bit shifting and the | operator should be the most efficient way of doing this.

    int i = (bytes[2] << 0) | (bytes[1] << 8) | (bytes[0] << 16);

    Also, as a heads up, you’re dropping the most significant byte, not the least significant byte ;p

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

Sidebar

Related Questions

I am working on a rather large WinForms application that has dozens of controls
I'm working on my first real WinForms application, and my boss has asked that
I'm working on a winforms application that is very complicated, and has massive callback
The context I'm working with a Winforms application (.NET 3.5 SP1) which has the
I am using VS2010, working on a WinForms application that uses the ReportViewer control
I am working on a C# WinForms application that uses some DevExpress controls. I
I've got a WinForms application that I am working on. There is one small
I'm working with a .NET 2.0 WinForms application in C#. I noticed something that
I am working on a Windows application (WinForms) that requires four different processes to
I have a Winforms application which is working fine.. using a BackgroundWorkerThread to manage

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.