-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
163 changed files
with
34,210 additions
and
24,536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
|
||
namespace MsmhToolsClass; | ||
|
||
public class ByteArrayTool | ||
{ | ||
public static int Search(byte[] src, byte[] pattern) | ||
{ | ||
int maxFirstCharSlot = src.Length - pattern.Length + 1; | ||
for (int i = 0; i < maxFirstCharSlot; i++) | ||
{ | ||
if (src[i] != pattern[0]) // compare only first byte | ||
continue; | ||
|
||
// found a match on first byte, now try to match rest of the pattern | ||
for (int j = pattern.Length - 1; j >= 1; j--) | ||
{ | ||
if (src[i + j] != pattern[j]) break; | ||
if (j == 1) return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
/// <summary> | ||
/// Fully read a stream into a byte array. | ||
/// </summary> | ||
/// <param name="input">The input stream.</param> | ||
/// <returns>A byte array containing the data read from the stream.</returns> | ||
public static byte[] StreamToBytes(Stream input) | ||
{ | ||
if (input == null) throw new ArgumentNullException(nameof(input)); | ||
if (!input.CanRead) throw new InvalidOperationException("Input stream is not readable"); | ||
|
||
byte[] buffer = new byte[16 * 1024]; | ||
using MemoryStream ms = new(); | ||
int read; | ||
|
||
while ((read = input.Read(buffer, 0, buffer.Length)) > 0) | ||
{ | ||
ms.Write(buffer, 0, read); | ||
} | ||
|
||
return ms.ToArray(); | ||
} | ||
|
||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,98 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics; | ||
|
||
#nullable enable | ||
namespace MsmhToolsClass | ||
namespace MsmhToolsClass; | ||
|
||
public class ConvertTool | ||
{ | ||
public class ConvertTool | ||
public static string TimeSpanToHumanRead(TimeSpan eTime, bool fullRead) | ||
{ | ||
public enum SizeUnits | ||
string days = eTime.Days.ToString().TrimStart('0').PadLeft(1, '0'); | ||
eTime = TimeSpan.FromMilliseconds(Math.Round(eTime.TotalMilliseconds, 2)); | ||
string hours = eTime.Hours.ToString().TrimStart('0').PadLeft(1, '0'); | ||
string minutes = eTime.Minutes.ToString().TrimStart('0').PadLeft(1, '0'); | ||
string seconds = eTime.Seconds.ToString().TrimStart('0').PadLeft(1, '0'); | ||
string milliseconds = eTime.Milliseconds.ToString().TrimStart('0').PadLeft(1, '0'); | ||
|
||
string result = string.Empty; | ||
if (fullRead) | ||
{ | ||
Byte, KB, MB, GB, TB, PB, EB, ZB, YB, RB, QB | ||
if (eTime.Days > 0) result += eTime.Days > 1 ? $"{days} Days, " : $"{days} Day, "; | ||
if (eTime.Hours > 0) result += eTime.Hours > 1 ? $"{hours} Hours, " : $"{hours} Hour, "; | ||
if (eTime.Minutes > 0) result += eTime.Minutes > 1 ? $"{minutes} Minutes, " : $"{minutes} Minute, "; | ||
if (eTime.Seconds > 0) result += eTime.Seconds > 1 ? $"{seconds} Seconds, " : $"{seconds} Second, "; | ||
if (eTime.Milliseconds > 0) result += eTime.Milliseconds > 1 ? $"{milliseconds} Milliseconds" : $"{milliseconds} Millisecond"; | ||
result = result.Trim(); | ||
if (result.EndsWith(',')) result = result.TrimEnd(','); | ||
} | ||
|
||
public static string ConvertByteToHumanRead(double bytes) | ||
else | ||
{ | ||
string result = $"{bytes} {SizeUnits.Byte}"; | ||
double calc = bytes; | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.Byte, SizeUnits.KB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.KB, SizeUnits.MB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.MB, SizeUnits.GB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.GB, SizeUnits.TB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.TB, SizeUnits.PB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.PB, SizeUnits.EB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.EB, SizeUnits.ZB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.ZB, SizeUnits.YB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.YB, SizeUnits.RB, out result); | ||
if (calc > 1000) ConvertSize(calc, SizeUnits.RB, SizeUnits.QB, out result); | ||
return result; | ||
if (eTime.Days > 0) result += $"{days}:"; | ||
if (eTime.Hours > 0) result += $"{hours}:"; | ||
if (eTime.Minutes > 0) result += $"{minutes}:"; | ||
if (eTime.Seconds > 0) result += $"{seconds}."; | ||
if (eTime.Milliseconds > 0) result += $"{milliseconds}"; | ||
if (result.EndsWith(':')) result = result.TrimEnd(':'); | ||
if (result.EndsWith('.')) result = result.TrimEnd('.'); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static double ConvertSize(double value, SizeUnits fromUnit, SizeUnits toUnit, out string humanRead) | ||
public enum SizeUnits | ||
{ | ||
Byte, KB, MB, GB, TB, PB, EB, ZB, YB, RB, QB | ||
} | ||
|
||
public static string ConvertByteToHumanRead(double bytes) | ||
{ | ||
bytes = Math.Round(bytes, 2); | ||
string result = $"{bytes} {SizeUnits.Byte}"; | ||
double calc = bytes; | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.Byte, SizeUnits.KB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.KB, SizeUnits.MB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.MB, SizeUnits.GB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.GB, SizeUnits.TB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.TB, SizeUnits.PB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.PB, SizeUnits.EB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.EB, SizeUnits.ZB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.ZB, SizeUnits.YB, out result); | ||
if (calc > 1000) calc = ConvertSize(calc, SizeUnits.YB, SizeUnits.RB, out result); | ||
if (calc > 1000) ConvertSize(calc, SizeUnits.RB, SizeUnits.QB, out result); | ||
return result; | ||
} | ||
|
||
public static double ConvertSize(double value, SizeUnits fromUnit, SizeUnits toUnit, out string humanRead) | ||
{ | ||
try | ||
{ | ||
try | ||
double unit = 1000; // In decimal it's 1000 not 1024. ref:https://en.wikipedia.org/wiki/Byte#Multiple-byte_units | ||
double valueByte = value * (double)Math.Pow(unit, (long)fromUnit); | ||
double outValue = valueByte / (double)Math.Pow(unit, (long)toUnit); | ||
string outString = outValue.ToString(); | ||
if (outString.Contains('.') && outString.EndsWith('0')) | ||
{ | ||
double unit = 1000; // In decimal it's 1000 not 1024. ref:https://en.wikipedia.org/wiki/Byte#Multiple-byte_units | ||
double valueByte = value * (double)Math.Pow(unit, (long)fromUnit); | ||
double outValue = valueByte / (double)Math.Pow(unit, (long)toUnit); | ||
string outString = outValue.ToString(); | ||
if (outString.Contains('.') && outString.EndsWith('0')) | ||
while (outString.EndsWith('0')) | ||
{ | ||
while (outString.EndsWith('0')) | ||
{ | ||
outString = outString.TrimEnd('0'); | ||
} | ||
outString = outString.TrimEnd('.'); | ||
double result = double.TryParse(outString, out double outValueNoZero) ? outValueNoZero : outValue; | ||
humanRead = $"{Math.Round(result, 2)} {toUnit}"; | ||
return result; | ||
} | ||
else | ||
{ | ||
humanRead = $"{Math.Round(outValue, 2)} {toUnit}"; | ||
return outValue; | ||
outString = outString.TrimEnd('0'); | ||
} | ||
outString = outString.TrimEnd('.'); | ||
double result = double.TryParse(outString, out double outValueNoZero) ? outValueNoZero : outValue; | ||
humanRead = $"{Math.Round(result, 2)} {toUnit}"; | ||
return result; | ||
} | ||
catch (Exception ex) | ||
else | ||
{ | ||
Debug.WriteLine($"ConvertSize: {ex.Message}"); | ||
humanRead = $"-1 {toUnit}"; | ||
return -1; | ||
humanRead = $"{Math.Round(outValue, 2)} {toUnit}"; | ||
return outValue; | ||
} | ||
} | ||
//----------------------------------------------------------------------------------- | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine($"ConvertSize: {ex.Message}"); | ||
humanRead = $"-1 {toUnit}"; | ||
return -1; | ||
} | ||
} | ||
} | ||
//----------------------------------------------------------------------------------- | ||
} |
Oops, something went wrong.