Skip to content

Commit 4b02f86

Browse files
committed
Added to/from Base64 string conversion
1 parent 2a5664d commit 4b02f86

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/MADE.Data.Converters/Extensions/StringExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MADE.Data.Converters.Extensions
55
{
6+
using System;
67
using System.Text;
78

89
/// <summary>
@@ -84,6 +85,33 @@ public static string ToDefaultCase(this string value)
8485
return result;
8586
}
8687

88+
/// <summary>
89+
/// Converts a value to a Base64 string using the specified encoding.
90+
/// <para>
91+
/// Default encoding is UTF-8.
92+
/// </para>
93+
/// </summary>
94+
/// <param name="value">The string value to convert.</param>
95+
/// <param name="encoding">The encoding to get the value bytes while converting.</param>
96+
/// <returns>The Base64 string representing the value.</returns>
97+
public static string ToBase64(this string value, Encoding encoding = default)
98+
{
99+
encoding ??= Encoding.UTF8;
100+
return Convert.ToBase64String(encoding.GetBytes(value));
101+
}
102+
103+
/// <summary>
104+
/// Converts a Base64 string to a value using the specified encoding.
105+
/// </summary>
106+
/// <param name="base64Value">The Base64 value to convert.</param>
107+
/// <param name="encoding">The encoding to get the value string while converting.</param>
108+
/// <returns>The string value representing the Base64 string.</returns>
109+
public static string FromBase64(this string base64Value, Encoding encoding = default)
110+
{
111+
encoding ??= Encoding.UTF8;
112+
return encoding.GetString(Convert.FromBase64String(base64Value));
113+
}
114+
87115
/// <summary>
88116
/// Converts a string value to an integer.
89117
/// </summary>

0 commit comments

Comments
 (0)