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:
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?