none
c# byte and bit conversion RRS feed

  • Question

  • 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


    • Edited by lctk Thursday, January 24, 2019 12:06 AM
    Wednesday, January 23, 2019 9:55 AM

Answers

  • 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);
                    }
                }
            }

    • Marked as answer by lctk Tuesday, February 26, 2019 12:26 AM
    Wednesday, January 23, 2019 10:59 AM
  • 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'.

    • Marked as answer by lctk Tuesday, February 26, 2019 12:26 AM
    Wednesday, January 23, 2019 11:40 PM
  • 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 3:15 AM

All replies

  • 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);
                    }
                }
            }

    • Marked as answer by lctk Tuesday, February 26, 2019 12:26 AM
    Wednesday, January 23, 2019 10:59 AM
  • 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'.

    • Marked as answer by lctk Tuesday, February 26, 2019 12:26 AM
    Wednesday, January 23, 2019 11:40 PM
  • 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:05 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.
    Thursday, January 24, 2019 12:10 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


    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 3:15 AM