Convert string of binary values to byte array

I have a string with binary values
(i.e. 0000000011001010111111110011111100111001101110001011011100110110001101011011010000110011101100101011000100111001101110001011011100110110001101011011010000110011101100101011000100111001101110001011011100110110001101011011010000110011101100101011000110111011111111110000000000000000000000000000000000000000000000000000000000000000)
that I need to convert to a unicode byte array.  

hess_joelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Do you need for each bit to be a byte in the array?

Bob
0
Bob LearnedCommented:
If you do, then try this:
            string s = "0000000011001010111111110011111100111001101110001011011100110110001101011011010000110011101100101011000100111001101110001011011100110110001101011011010000110011101100101011000100111001101110001011011100110110001101011011010000110011101100101011000110111011111111110000000000000000000000000000000000000000000000000000000000000000";
            List<byte> list = new List<byte>();
            foreach (char ch in s.ToCharArray())
            {
                list.Add((byte)((int)ch - 48));
            }
 
            byte[] byteArray = list.ToArray();

Open in new window

0
hess_joelAuthor Commented:
No, it's 8 bits per byte.
0
Bob LearnedCommented:
Try this:
        public byte[] GetBytesFromBinaryString(string binary, int bitsPerByte)
        {
            List<byte> list = new List<byte>();
            for (int i = 0; i < binary.Length; i += bitsPerByte)
            {
                string t = binary.Substring(i, bitsPerByte);
 
                list.Add(Convert.ToByte(t, 2));
            }
            return list.ToArray();
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
using System;
using System.Collections.Generic;

Bob

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.