diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs index 662146c25e3..7732c19d900 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs @@ -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) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index 14accff2a88..c2f0b9a9740 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -454,6 +454,53 @@ namespace Godot return hashv; } + /// + /// Returns a hexadecimal representation of this byte as a string. + /// + /// The byte to encode. + /// The hexadecimal representation of this byte. + 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; + } + + /// + /// Returns a hexadecimal representation of this byte array as a string. + /// + /// The byte array to encode. + /// The hexadecimal representation of this byte array. + public static string HexEncode(this byte[] bytes) + { + var ret = string.Empty; + + foreach (byte b in bytes) + { + ret += b.HexEncode(); + } + + return ret; + } + // // Convert a string containing an hexadecimal number into an int. //