Skip to content

Commit 6c2054e

Browse files
authored
feat(Socket): HexConvert method support upper and lower case (#546)
* refactor: 精简代码逻辑 * refactor: 代码格式化 * feat: 增加 upper 参数支持大小写
1 parent 9fe0f1e commit 6c2054e

2 files changed

Lines changed: 4 additions & 11 deletions

File tree

src/extensions/BootstrapBlazor.Socket/DataConverter/HexConverter.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5-
using System.Text;
6-
75
namespace BootstrapBlazor.Components.DataConverter;
86

97
/// <summary>
@@ -17,8 +15,9 @@ public static class HexConverter
1715
/// </summary>
1816
/// <param name="bytes">The byte array to convert.</param>
1917
/// <param name="separator"></param>
18+
/// <param name="upper"></param>
2019
/// <returns>A string containing the hexadecimal representation of the byte array.</returns>
21-
public static string ToString(byte[]? bytes, string? separator = "-")
20+
public static string ToString(byte[]? bytes, string? separator = "-", bool upper = true)
2221
{
2322
if (bytes == null || bytes.Length == 0)
2423
{
@@ -30,13 +29,7 @@ public static string ToString(byte[]? bytes, string? separator = "-")
3029
return BitConverter.ToString(bytes);
3130
}
3231

33-
var sb = new StringBuilder(bytes.Length * 3);
34-
foreach (var b in bytes)
35-
{
36-
sb.Append(b.ToString("X2"));
37-
sb.Append(separator);
38-
}
39-
return sb.ToString(0, sb.Length - 1);
32+
return string.Join(separator, bytes.Select(i => upper ? i.ToString("X2") : i.ToString("x2")));
4033
}
4134

4235
/// <summary>

src/extensions/BootstrapBlazor.Socket/Extensions/DataPropertyExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static class DataPropertyExtensions
1919
{
2020
var converterParameters = attribute.ConverterParameters;
2121
var c = Activator.CreateInstance(converterType, converterParameters);
22-
if(c is IDataPropertyConverter v)
22+
if (c is IDataPropertyConverter v)
2323
{
2424
converter = v;
2525
}

0 commit comments

Comments
 (0)