Files
UCS_schermsoftware/UCS.Stentofon/Helpers.cs
2021-04-16 08:13:01 +02:00

64 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UCS.Stentofon
{
public static class Helpers
{
public static string ByteArrayToString(byte[] ba)
{
return BitConverter.ToString(ba, 0).Replace("-", " ");
}
public static string ByteArrayToString(byte[] ba, int len)
{
return BitConverter.ToString(ba, 0, len).Replace("-", " ");
}
public static string ByteArrayToStringWithoutSpaces(byte[] ba)
{
return BitConverter.ToString(ba, 0).Replace("-", "");
}
public static string IntToBinaryString(int number)
{
const int mask = 1;
var binary = string.Empty;
while (number > 0)
{
// Logical AND the number and prepend it to the result string
binary = (number & mask) + binary;
number >>= 1;
}
return binary;
}
public static uint GetOptionSize(byte val)
{
//3 upper bits(mask: 0xE0)
return (uint)((val & 0xE0) >> 5);
}
public static uint GetHopCount(byte val)
{
//5 lower bits(mask: 0x1F)
return (uint)(val & 0x1F);
}
public static Types.UINT1 CreateHopCountByte(uint optionSize, uint HopCounter)
{
//OptionSize is 3 upper bits
//Hopcounter is 5 lower bits
byte val = (byte)optionSize;
val = (byte)(val << 5);
val = (byte)(val | HopCounter);
return new Types.UINT1(val);
}
}
}