Skip to content

Commit d9d8b3d

Browse files
committed
💾 Feat(Ext): New functions to convert string into bytes in specific encoding.
1 parent 03a2f9e commit d9d8b3d

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Common.BasicHelper/Utils/Extensions/StringHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,32 @@ public static void Throw<T>(this string? message) where T : Exception
131131
/// <param name="path">路径</param>
132132
/// <returns>给定路径的完整字节数</returns>
133133
public static long GetTotalSize(this string path) => DirectoryHelper.GetDirectorySize(path);
134+
135+
/// <summary>
136+
/// 使用 UTF8 编码将字符串转换为字节数组
137+
/// </summary>
138+
/// <param name="text">字符串</param>
139+
/// <returns>字节数组</returns>
140+
public static byte[] FromUTF8(this string text) => Encoding.UTF8.GetBytes(text);
141+
142+
/// <summary>
143+
/// 使用 UTF32 编码将字符串转换为字节数组
144+
/// </summary>
145+
/// <param name="text">字符串</param>
146+
/// <returns>字节数组</returns>
147+
public static byte[] FromUTF32(this string text) => Encoding.UTF32.GetBytes(text);
148+
149+
/// <summary>
150+
/// 使用 Unicode 编码将字符串转换为字节数组
151+
/// </summary>
152+
/// <param name="text">字符串</param>
153+
/// <returns>字节数组</returns>
154+
public static byte[] FromUnicode(this string text) => Encoding.Unicode.GetBytes(text);
155+
156+
/// <summary>
157+
/// 使用 ASCII 编码将字符串转换为字节数组
158+
/// </summary>
159+
/// <param name="text">字符串</param>
160+
/// <returns>字节数组</returns>
161+
public static byte[] FromASCII(this string text) => Encoding.ASCII.GetBytes(text);
134162
}

0 commit comments

Comments
 (0)