Share via


c# byte and bit conversion

Question

Wednesday, January 23, 2019 9:55 AM

A byte = 8bit, how to convert each 2 digits into a number?

Unknow0   2bit

I mean to convert a byte into 4 numbers.1byte=1,2,3,2

please verify my account

All replies (6)

Wednesday, January 23, 2019 10:59 AM ✅Answered

You can take this code. But there is fail when first bit is zero.

static void Main(string[] args)
        {
            byte b = 94;
            byte k = 0;
            for(int i = 0; i<8; i++)
            {
                if (i > 0 && i % 2 == 0)
                {
                    Console.WriteLine(k >> 1);
                    k = 0;
                }
                if ((int)(b & (1 << i)) != 0)
                {
                    k++;
                    k = (byte)(k << 1);
                }
            }
        }

Wednesday, January 23, 2019 11:40 PM ✅Answered

I have an alternative procedure which SEEMS to work. No guarantees, of course.

You did not specify the ordering you seek, so I did both: strings 'ascending' and 'descending'.

The terms 'ascending' and 'descending' refer to the ordering of the power-of-4, so that 'ascending' begins with the least significant 2 bits, and progresses to the 2 most significant bits (i.e. right-to-left).
'descending' begins with the most significant 2 bits, and progresses to the 2 least significant bits (i.e. left-to-right).  I THINK.

Here's the code ..

        static void Main(string[] args)
        {
            byte b = 94;
            byte c = 94;

            byte k = 0;
            for (int i = 0; i < 8; i++)
            {
                if (i > 0 && i % 2 == 0)
                {
                    Console.WriteLine(k >> 1);
                    k = 0;
                }
                if ((int)(b & (1 << i)) != 0)
                {
                    k++;
                    k = (byte)(k << 1);
                }
            }

            // *****************************************************
            // Alternative procedure
            // *****************************************************
            string ascending = string.Empty, descending = string.Empty;
            int accumulator;
            const int ascending_mask = 3;
            const int descending_mask = 0xc0;
            const string space = " ";

            for (int i = 0; i < 4; i++)
            {
                accumulator = b & ascending_mask;
                ascending += accumulator.ToString() + space;
                b >>= 2;

                accumulator = (c & descending_mask) >> (6);
                descending += space + accumulator;
                c <<= 2;

            }
            Console.WriteLine(ascending);
            Console.WriteLine(descending);
            // *****************************************************
        }
    }

Note that for my convenience, I chose to put the results in a string. Your app can remove those strings and simply take the contents of 'accumulator'.


Thursday, January 24, 2019 3:15 AM ✅Answered

A byte = 8bit, how to convert each 2 digits into a number?

Unknow0   2bit

I mean to convert a byte into 4 numbers.1byte=1,2,3,2

please verify my account

Please, don't post question soooooooooooooooooooooooo difficult.

It takes more than 1 line to solve it (in fact it can be made just 1):

public class Program
{
    public static void Main(string[] args)
    {
        byte b=110; // 110 = 0x6E = 0110.1110 => 1232

        for(int m=3,i=0;i<4;b>>=2,++i) // from lowest to highest
            Console.WriteLine(b&m);

        Console.WriteLine("\nHello, world!"); // bonus
    }
}

Please, notice that I added the last line as a bonus!

Tanks.


Thursday, January 24, 2019 12:05 AM

Hello, a byte=4bit, 2 bits to form a number, should be 4 numbers?

please verify my account


Thursday, January 24, 2019 12:05 AM

Hello, a byte=4bit, 2 bits to form a number, should be 4 numbers?

please verify my account


Thursday, January 24, 2019 12:10 AM

well, I'm thinkin' that a byte is 8 bits. And you wanted two-bit fields within the 8 bytes. The alternate procedure provides (8/2) 4 numbers in the strings. … At least, it does so on my 'chine.