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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:17:02+00:00 2026-05-11T13:17:02+00:00

I have an array of 128 booleans that represent bits. How can I convert

  • 0

I have an array of 128 booleans that represent bits. How can I convert these 128 bit representations into 16 bytes?

Example:

I have an array that looks like this:

0110001100110000100010111011001011010011010001010001101101001100 1000010000000000001000111111111101000011111001111011111011111001 

(Converted to 1s and 0s to be more concise)

I need to convert those bits to the following byte array:

99 48 139 178 211 69 27 76 132 0 35 255 67 231 190 249 

EDIT: This doesn’t seem to work:

public byte[] ToByteArray() {     int numBytes = Count / 8;      if (_bits.Count % 8 != 0) numBytes++;      byte[] bytes = new byte[numBytes];      int byteIndex = 0, bitIndex = 0;      for (int i = 0; i < _bits.Count; i++) {         if (_bits[i])             bytes[byteIndex] |= (byte)(1 << bitIndex);          bitIndex++;         if (bitIndex == 8) {             bitIndex = 0;             byteIndex++;         }     }      return bytes; } 

It outputs:

198 12 209 77 203 162 216 50 33 0 196 255 194 231 125 159 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T13:17:02+00:00Added an answer on May 11, 2026 at 1:17 pm

    The code is treating the first bit as the low bit of the word, so you end up with each word reversed. As a quick-and-dirty fix, try this:

    bytes[byteIndex] |= (byte)(1 << (7-bitIndex)); 

    That puts the first bit in the array at the highest position in the first byte, etc.

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

Sidebar

Related Questions

I have an array of numbers that potentially have up to 8 decimal places
I have an array of items that are time sensitive. After an amount of
I have an array of 1000 strings to load into a combo box. What
I have an array of shorts (short[]) that I need to write out to
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
I have following array, that I need to operate by hand on bitmaps. const
I am looking to display an array from 128 to 512 bytes in size
How do i explode this string '||25||34||73||94||116||128' i need to have a array like
I have array and contains 50 values , example [ 'london', 'bluehit', 'green', 'lonpark',
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove

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.