mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2026-04-07 10:11:10 +00:00
Added some utils methods
This commit is contained in:
parent
aa1ebdc4ed
commit
177daf903b
@ -442,6 +442,14 @@ namespace CryptoExchange.Net.Authentication
|
||||
/// <param name="buff"></param>
|
||||
/// <returns></returns>
|
||||
protected static string BytesToHexString(byte[] buff)
|
||||
=> BytesToHexString(new ArraySegment<byte>(buff));
|
||||
|
||||
/// <summary>
|
||||
/// Convert byte array to hex string
|
||||
/// </summary>
|
||||
/// <param name="buff"></param>
|
||||
/// <returns></returns>
|
||||
protected static string BytesToHexString(ArraySegment<byte> buff)
|
||||
{
|
||||
#if NET9_0_OR_GREATER
|
||||
return Convert.ToHexString(buff);
|
||||
@ -453,6 +461,26 @@ namespace CryptoExchange.Net.Authentication
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a hex encoded string to byte array
|
||||
/// </summary>
|
||||
/// <param name="hexString"></param>
|
||||
/// <returns></returns>
|
||||
protected static byte[] HexToBytesString(string hexString)
|
||||
{
|
||||
if (hexString.StartsWith("0x"))
|
||||
hexString = hexString.Substring(2);
|
||||
|
||||
byte[] bytes = new byte[hexString.Length / 2];
|
||||
for (int i = 0; i < hexString.Length; i += 2)
|
||||
{
|
||||
string hexSubstring = hexString.Substring(i, 2);
|
||||
bytes[i / 2] = Convert.ToByte(hexSubstring, 16);
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert byte array to base64 string
|
||||
/// </summary>
|
||||
|
||||
@ -242,8 +242,7 @@ namespace CryptoExchange.Net
|
||||
/// <summary>
|
||||
/// Generate a long value
|
||||
/// </summary>
|
||||
/// <param name="maxLength">Max character length</param>
|
||||
/// <returns></returns>
|
||||
/// <param name="maxLength">Max number of digits</param>
|
||||
public static long RandomLong(int maxLength)
|
||||
{
|
||||
#if NETSTANDARD2_1_OR_GREATER || NET9_0_OR_GREATER
|
||||
@ -259,6 +258,19 @@ namespace CryptoExchange.Net
|
||||
return value;
|
||||
}
|
||||
|
||||
public static long RandomLong(long minValue, long maxValue)
|
||||
{
|
||||
#if NET8_0_OR_GREATER
|
||||
var buf = RandomNumberGenerator.GetBytes(8);
|
||||
#else
|
||||
byte[] buf = new byte[8];
|
||||
var random = new Random();
|
||||
random.NextBytes(buf);
|
||||
#endif
|
||||
long longRand = BitConverter.ToInt64(buf, 0);
|
||||
return (Math.Abs(longRand % (maxValue - minValue)) + minValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a random string of specified length
|
||||
/// </summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user