Add HexEncode to C#

(cherry picked from commit 6b54d7dde1)
This commit is contained in:
Aaron Franke 2020-10-31 21:12:57 -05:00 committed by Rémi Verschelde
parent e3419a7fe1
commit 3b10458a5d
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 50 additions and 24 deletions

View File

@ -695,31 +695,10 @@ namespace Godot
return ig;
}
private String ToHex32(float val)
private string ToHex32(float val)
{
int v = Mathf.RoundToInt(Mathf.Clamp(val * 255, 0, 255));
var ret = string.Empty;
for (int i = 0; i < 2; i++)
{
char c;
int lv = v & 0xF;
if (lv < 10)
{
c = (char)('0' + lv);
}
else
{
c = (char)('a' + lv - 10);
}
v >>= 4;
ret = c + ret;
}
return ret;
byte b = (byte)Mathf.RoundToInt(Mathf.Clamp(val * 255, 0, 255));
return b.HexEncode();
}
internal static bool HtmlIsValid(string color)

View File

@ -454,6 +454,53 @@ namespace Godot
return hashv;
}
/// <summary>
/// Returns a hexadecimal representation of this byte as a string.
/// </summary>
/// <param name="bytes">The byte to encode.</param>
/// <returns>The hexadecimal representation of this byte.</returns>
internal static string HexEncode(this byte b)
{
var ret = string.Empty;
for (int i = 0; i < 2; i++)
{
char c;
int lv = b & 0xF;
if (lv < 10)
{
c = (char)('0' + lv);
}
else
{
c = (char)('a' + lv - 10);
}
b >>= 4;
ret = c + ret;
}
return ret;
}
/// <summary>
/// Returns a hexadecimal representation of this byte array as a string.
/// </summary>
/// <param name="bytes">The byte array to encode.</param>
/// <returns>The hexadecimal representation of this byte array.</returns>
public static string HexEncode(this byte[] bytes)
{
var ret = string.Empty;
foreach (byte b in bytes)
{
ret += b.HexEncode();
}
return ret;
}
// <summary>
// Convert a string containing an hexadecimal number into an int.
// </summary>