using System.Collections;
public static byte[] ToByteArray(String hexString)
byte[] retval = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
retval[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
public static string ToHexString(byte[] bytes) => BitConverter.ToString(bytes).Replace("-", string.Empty);
public static byte SetBit(byte val, int pos, bool one) => one ?(byte)(val | (1 << pos)) : (byte)(val & ~(1 << pos));
public static bool GetBit(byte val, int pos) => ((val & (1 << pos)) != 0);
public static Int32 SetBit(Int32 val, int pos, bool one) => one ?(byte)(val | (1 << pos)) : (byte)(val & ~(1 << pos));
public static bool GetBit(Int32 val, int pos) => (val & (1 << pos)) != 0;
public static byte[] SetBit(byte[] b, int pos, bool value) {
byte[] bytes = new byte[b.Length];
int byteIndex = bytes.Length - 1 - (pos / 8);
int bitInByteIndex = pos % 8;
bytes[byteIndex] = SetBit(bytes[byteIndex], bitInByteIndex, value);
public static void Main()
string hexVal = "00AAFFCC";
byte[] byteVal = BitConverter.GetBytes(intVal);
BitArray bits = new BitArray(byteVal);
public static string joinHex(string h1, string h2){
byte[] b1 = ToByteArray(h1);
byte[] b2 = ToByteArray(h2);
byte[] b3 = new byte[b1.Length];
for(int i = 0; i < b3.Length; i++){
b3[i] = (byte)(b1[i] | b2[i]);
Console.WriteLine("i: " + i + ", " + ToHexString(b3));