2using System.Collections;
3using System.Collections.Generic;
39 public static IEnumerable<byte>
HexToBytes(IEnumerable<char> seq)
41 using var enumerator = seq.GetEnumerator();
43 while (enumerator.MoveNext())
45 if (enumerator.Current is
'-' or
' ')
47 if (!enumerator.MoveNext())
throw new InvalidOperationException(
"Not a valid hex string");
50 var a = enumerator.Current.HexToInt();
51 if (!enumerator.MoveNext())
throw new InvalidOperationException(
"Not a valid hex string");
52 var b = enumerator.Current.HexToInt();
57 yield
return ((
byte)((a << 4) | b));
68 var builder =
new StringBuilder();
69 foreach (
bool bit
in arr)
71 builder.Append(bit ?
"1" :
"0");
74 return builder.ToString();
Extension methods for helping with binary conversion.
static byte ReverseBits(this byte value)
Reverse the bits of a single byte.
Helper routines for manipulating binary representations of data that are used within the OSDP....
static string BitArrayToString(BitArray arr)
Convert the BitArray instance to a binary string representation.
static BitArray HexStringToBitArray(IEnumerable< char > hex)
Convert a sequence of hex digit characters into BitArray.
static BitArray HexStringToBitArray(string hex)
Convert a string containing hex digits into BitArray.
static IEnumerable< byte > HexToBytes(IEnumerable< char > seq)
Convert a sequence of hex digit characters into their byte representation.