Results 1 to 4 of 4

Thread: How to convert bytes to bits

  1. #1
    Join Date
    Nov 2008
    Posts
    1,022

    How to convert bytes to bits

    I am creating a small program in C# to convert bytes to bits. This program then will be used in some other program. But the problem is that I am unable to generate the logic that can be used. I mean what should be the logic for the program. Any ideas how to convert bytes to bits?

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: How to convert bytes to bits

    Why don't you use some online tools such as Byte Converter? This tool will convert file storage size from one unit of measurement to another. The results will return all possible conversion options (bits, bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, and exabytes). For more information, visit this.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,221

    Re: How to convert bytes to bits

    Here is the basic code:

    Code:
    for (int i = 0; i < fReal.Length; i++)
    {
    Int16 temp = (Int16)data[i];
    byte high = Convert.ToByte((temp >> 8) & 0xff);
    byte low = Convert.ToByte(temp & 0xff);
    sw.Write(high);
    sw.Write(low);
    }

  4. #4
    Join Date
    Nov 2008
    Posts
    996

    Re: How to convert bytes to bits

    My piece of code would be:

    Code:
    public byte[] ToByteArray(this bool[] myNumber)
    {
        var bytes = new byte[myNumber.Length / 8];
        for (int i = 0, j = 0; j < myNumber.Length; i++, j += 8)
        {
            for (int offset = 0; offset < 8; offset++)
                bytes[i] |= (myNumber[j + offset] << offset);
        }
        return bytes;
    }

Similar Threads

  1. SanDisk flash drive “0 bytes used and 0 bytes free”
    By Coloma in forum Hardware Peripherals
    Replies: 5
    Last Post: 03-12-2012, 10:56 AM
  2. Replies: 5
    Last Post: 30-01-2012, 05:42 PM
  3. Convert IP address to array of bytes
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 11:54 AM
  4. How to convert Bytes to Megabytes
    By Girish-S in forum India BroadBand
    Replies: 2
    Last Post: 15-06-2009, 02:47 PM
  5. Convert a string to bytes
    By $tatic in forum Software Development
    Replies: 4
    Last Post: 18-02-2009, 02:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,691,738.63089 seconds with 16 queries