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 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

How do I convert a byte array into a string? I have found these
I have a 128-byte (1024-bit) modulus (in a byte array format) and my exponent
I'm trying to convert a 128-bit unsigned integer stored as an array of 4
Lets say that I have an array of 4 32-bit integers which I use
I have the following code that decodes a JSON string into an array of
Artichoke101 asked this : Lets say that I have an array of 4 32-bit
I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
I have a datatype that's more or less a character array. Each space in
I have an array result that contains values from 0-255. I originally declared it
I have a big array containing lots of elements containing numerical data. Example: 3200

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.