OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
BinaryExtensions.cs
1using System;
2
4{
8 public static class BinaryExtensions
9 {
15 public static byte ReverseBits(this byte value)
16 {
17 int c = (byte)((value >> 4) | ((value & 0xf) << 4));
18 c = ((c & 0xcc) >> 2) | ((c & 0x33) << 2);
19 c = ((c & 0xaa) >> 1) | ((c & 0x55) << 1);
20 return (byte)c;
21 }
22
28 public static bool IsHex(this char c) => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F');
29
36 public static int HexToInt(this char value) => value switch
37 {
38 { } c when (c >= '0' && c <= '9') => (byte)(c - '0'),
39 { } c when (c >= 'A' && c <= 'F') => (byte)(c - 'A' + 10),
40 { } c when (c >= 'a' && c <= 'f') => (byte)(c - 'a' + 10),
41 _ => throw new InvalidOperationException("Not a valid hex string")
42 };
43 }
44}
Extension methods for helping with binary conversion.
static bool IsHex(this char c)
Tests to see if character is a valid hex digit.
static int HexToInt(this char value)
Converts a single hex digit character to its integer representation.
static byte ReverseBits(this byte value)
Reverse the bits of a single byte.