2025-07-05 22:26:42 +02:00

68 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DVBServices
{
internal class Utils
{
/// <summary>
/// Convert a string of bytes to a hex string.
/// </summary>
/// <param name="inputChars">The string to be converted.</param>
/// <returns>The string of hex characters.</returns>
public static string ConvertToHex(byte[] inputChars)
{
return (ConvertToHex(inputChars, inputChars.Length));
}
/// <summary>
/// Convert a string of bytes to a hex string.
/// </summary>
/// <param name="inputChars">The array holding the bytes to be converted.</param>
/// <param name="length">The number of byte to be converted.</param>
/// <returns>The string of hex characters.</returns>
public static string ConvertToHex(byte[] inputChars, int length)
{
return (ConvertToHex(inputChars, 0, length));
}
/// <summary>
/// Convert a string of bytes to a hex string.
/// </summary>
/// <param name="inputChars">The array holding the bytes to be converted.</param>
/// <param name="offset">The the offset to the first byte to be converted.</param>
/// <param name="length">The number of byte to be converted.</param>
/// <returns>The string of hex characters.</returns>
public static string ConvertToHex(byte[] inputChars, int offset, int length)
{
char[] outputChars = new char[length * 2];
int outputIndex = 0;
for (int inputIndex = 0; inputIndex < length; inputIndex++)
{
int hexByteLeft = inputChars[offset] >> 4;
int hexByteRight = inputChars[offset] & 0x0f;
outputChars[outputIndex] = getHex(hexByteLeft);
outputChars[outputIndex + 1] = getHex(hexByteRight);
outputIndex += 2;
offset++;
}
return ("0x" + new string(outputChars));
}
private static char getHex(int value)
{
if (value < 10)
return ((char)('0' + value));
return ((char)('a' + (value - 10)));
}
}
}