Szukam sposobu przekonwertowanie ciągu binarnego na byte[] array

0

Witam serdecznie. 32 bity zapisane w ciagu np "10100101010010101010101001001010" muszę zamienić na byte[4]
Moja metoda wygląda tak:

public byte[] SelectAreas(int[] Areas)
        {
            string s = "";
            for (int n = 32; n > 0; n--)
               s += Areas.Contains(n) ? "1" : "0";
            byte buff4 = Convert.ToByte(s.Substring(0, 8));
            byte buff3 = Convert.ToByte(s.Substring(8, 8));
            byte buff2 = Convert.ToByte(s.Substring(16, 8));
            byte buff1 = Convert.ToByte(s.Substring(24, 8));
            return new byte[] { buff1, buff2, buff3, buff4 };
        }

Metoda ma przygotować część ramki wysyłanej do centrali Integra. Bity układam według poniższego obrazka:
screenshot-20201207140620.png

Otrzymuję wyjątek w Convert.ToByte(s.Substring(0, 8));
System.OverflowException: „Wartość jest za duża albo za mała dla bajtu bez znaku.”

Ten wyjątek został pierwotnie zgłoszony w tym stosie wywołań:
byte.Parse(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
System.Convert.ToByte(string)
Integra.IntegraRS.Command.SelectAreas(int[]) w Command.cs
Integra.MainWindow.hInput_PreviewKeyDown(object, System.Windows.Input.KeyEventArgs) w MainWindow.xaml.cs
System.Windows.Input.KeyEventArgs.InvokeEventHandler(System.Delegate, object)
System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, object)
System.Windows.RoutedEventHandlerInfo.InvokeHandler(object, System.Windows.RoutedEventArgs)
System.Windows.EventRoute.InvokeHandlersImpl(object, System.Windows.RoutedEventArgs, bool)
System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
...
[Obcięto stos wywołań]

Moglibyście podpowiedzieć co jest nie tak?

0

Wszystko jasne, źle konwertowałem. Tak jest dobrze

private static byte[] StringBinToByteArray(string bin)
        {
            var byteArray = Enumerable.Range(0, int.MaxValue / 8)
                          .Select(i => i * 8)    // get the starting index of which char segment
                          .TakeWhile(i => i < bin.Length)
                          .Select(i => bin.Substring(i, 8)) // get the binary string segments
                          .Select(s => Convert.ToByte(s, 2)) // convert to byte
                          .ToArray();
            return byteArray;
        }
/// <summary>
        /// Określa na podstawie 4 bajtów wybór stref od 1 do 32
        /// </summary>
        /// <param name="Areas">Strefy</param>
        /// <returns></returns>
        public byte[] SelectAreas(int[] Areas)
        {
            string s = "";
            for (int n = 32; n > 0; n--)
                s += Areas.Contains(n) ? "1" : "0";
            var array = StringBinToByteArray(s);
            return new byte[] { array[3], array[2], array[1], array[0] };
        }
0

Nie do końca rozumiem idea masz tablice intów np

int[] tab = { 1, 0, 1, 0, 1, 0, 1, 1, 1, 0 };

Z 32 elementami tak i chcesz to zamienić na tablicę 4 bajtów? No bo skoro tak to to co robisz nie ma do końca sensu. Contains sprawdza czy dana liczba znajduje się w całym tablicy a nie tylko na danej pozycji więc to nie da ci zamierzonego rezultatu bo zawsze zwróci true.

1 użytkowników online, w tym zalogowanych: 0, gości: 1