38 lines
770 B
C#
38 lines
770 B
C#
namespace NubLang;
|
|
|
|
public static class HexString
|
|
{
|
|
private static readonly HashSet<string> _used = [];
|
|
|
|
private static readonly char[] StringChars = "0123456789abcdef".ToArray();
|
|
|
|
public static string CreateUnique(int length)
|
|
{
|
|
string hex;
|
|
|
|
while (true)
|
|
{
|
|
hex = Create(length);
|
|
if (_used.Add(hex))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return hex;
|
|
}
|
|
|
|
public static string Create(int length)
|
|
{
|
|
var rand = new Random();
|
|
var hexString = "";
|
|
|
|
for (var i = 0; i < length; i++)
|
|
{
|
|
var randIndex = rand.Next(0, StringChars.Length);
|
|
hexString += StringChars[randIndex];
|
|
}
|
|
|
|
return hexString;
|
|
}
|
|
} |