Skip to content

Commit 9fe0f1e

Browse files
authored
feat(Converter): add HexConverter/BinConverter static class (#544)
* feat: 增加 HexConverter 类 * test: 增加单元测试 * refactor: 更新 HexConveter 文档注释 * feat: 增加 BinConverter 转换器 * doc: 更新文档 * test: 更新单元测试 * chore: bump version 9.0.2
1 parent e4236c0 commit 9fe0f1e

5 files changed

Lines changed: 244 additions & 1 deletion

File tree

src/extensions/BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Version>9.0.1</Version>
4+
<Version>9.0.2</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text;
6+
7+
namespace BootstrapBlazor.Components.DataConverter;
8+
9+
/// <summary>
10+
/// 二进制 与 Byte 数组转换方法
11+
/// </summary>
12+
public static class BinConverter
13+
{
14+
/// <summary>
15+
/// 将 byte[] 转为 二进制字符串
16+
/// <para>Converts a byte array to its hexadecimal string representation.</para>
17+
/// </summary>
18+
/// <param name="bytes">The byte array to convert.</param>
19+
/// <param name="separator"></param>
20+
/// <returns>A string containing the hexadecimal representation of the byte array.</returns>
21+
public static string ToString(byte[]? bytes, string? separator = "-")
22+
{
23+
if (bytes == null || bytes.Length == 0)
24+
{
25+
return string.Empty;
26+
}
27+
28+
return string.Join(separator, bytes.Select(i => Convert.ToString(i, 2).PadLeft(8, '0')));
29+
}
30+
31+
/// <summary>
32+
/// 将字符串转换为字节数组
33+
/// </summary>
34+
/// <param name="str"></param>
35+
/// <param name="separator"></param>
36+
/// <param name="options"></param>
37+
/// <returns></returns>
38+
public static byte[] ToBytes(string str, string? separator = null, StringSplitOptions options = StringSplitOptions.None)
39+
{
40+
// 把 str 内的 separator 符号替换掉
41+
if (!string.IsNullOrEmpty(separator))
42+
{
43+
str = string.Join("", str.Split(separator, options).Select(i => i.PadLeft(8, '0')));
44+
}
45+
46+
// 把 Hex 形式的 str 转化为 byte[]
47+
if (str.Length % 8 != 0)
48+
{
49+
throw new ArgumentException("The raw string cannot have an odd number of digits. 参数 str 位数不正确无法转化为 二进制字节数组", nameof(str));
50+
}
51+
52+
var bytes = new byte[str.Length / 8];
53+
for (var i = 0; i < bytes.Length; i++)
54+
{
55+
bytes[i] = Convert.ToByte(str.Substring(i * 8, 8), 2);
56+
}
57+
return bytes;
58+
}
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text;
6+
7+
namespace BootstrapBlazor.Components.DataConverter;
8+
9+
/// <summary>
10+
/// 十六进制 与 Byte 数组转换方法
11+
/// </summary>
12+
public static class HexConverter
13+
{
14+
/// <summary>
15+
/// 将 byte[] 转为 16 进制字符串
16+
/// <para>Converts a byte array to its hexadecimal string representation.</para>
17+
/// </summary>
18+
/// <param name="bytes">The byte array to convert.</param>
19+
/// <param name="separator"></param>
20+
/// <returns>A string containing the hexadecimal representation of the byte array.</returns>
21+
public static string ToString(byte[]? bytes, string? separator = "-")
22+
{
23+
if (bytes == null || bytes.Length == 0)
24+
{
25+
return string.Empty;
26+
}
27+
28+
if (separator == "-")
29+
{
30+
return BitConverter.ToString(bytes);
31+
}
32+
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);
40+
}
41+
42+
/// <summary>
43+
/// 将字符串转换为字节数组
44+
/// </summary>
45+
/// <param name="str"></param>
46+
/// <param name="separator"></param>
47+
/// <param name="options"></param>
48+
/// <returns></returns>
49+
public static byte[] ToBytes(string str, string? separator = null, StringSplitOptions options = StringSplitOptions.None)
50+
{
51+
// 把 str 内的 delimiter 符号替换掉
52+
if (!string.IsNullOrEmpty(separator))
53+
{
54+
str = string.Join("", str.Split(separator, options));
55+
}
56+
57+
// 把 Hex 形式的 str 转化为 byte[]
58+
if (str.Length % 2 != 0)
59+
{
60+
throw new ArgumentException("The raw string cannot have an odd number of digits. 参数 str 位数不正确无法转化为 16 进制字节数组", nameof(str));
61+
}
62+
63+
var bytes = new byte[str.Length / 2];
64+
for (var i = 0; i < bytes.Length; i++)
65+
{
66+
bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
67+
}
68+
return bytes;
69+
}
70+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using BootstrapBlazor.Components.DataConverter;
6+
7+
namespace UnitTestTcpSocket;
8+
9+
public class BinConverterTest
10+
{
11+
[Fact]
12+
public void ToHexString_Null()
13+
{
14+
var actual = BinConverter.ToString(null);
15+
Assert.Equal(string.Empty, actual);
16+
17+
actual = BinConverter.ToString([]);
18+
Assert.Equal(string.Empty, actual);
19+
}
20+
21+
[Fact]
22+
public void ToBinString_Ok()
23+
{
24+
var data = new byte[] { 0x1A, 0x02 };
25+
var actual = BinConverter.ToString(data);
26+
Assert.Equal("00011010-00000010", actual);
27+
28+
actual = BinConverter.ToString(data, " ");
29+
Assert.Equal("00011010 00000010", actual);
30+
}
31+
32+
[Fact]
33+
public void ToHexString_Exception()
34+
{
35+
var data = "00011010-00000010";
36+
var ex = Assert.ThrowsAny<ArgumentException>(() => BinConverter.ToBytes(data));
37+
Assert.NotNull(ex);
38+
}
39+
40+
[Fact]
41+
public void ToBytes_Ok()
42+
{
43+
var excepted = new byte[] { 0x1A, 0x02 };
44+
45+
var data = "00011010-00000010";
46+
var actual = BinConverter.ToBytes(data, "-");
47+
Assert.Equal(excepted, actual);
48+
49+
data = "00011010 00000010";
50+
actual = BinConverter.ToBytes(data, " ");
51+
Assert.Equal(excepted, actual);
52+
53+
data = "0001101000000010";
54+
actual = BinConverter.ToBytes(data);
55+
Assert.Equal(excepted, actual);
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using BootstrapBlazor.Components.DataConverter;
6+
7+
namespace UnitTestTcpSocket;
8+
9+
public class HexConverterTest
10+
{
11+
[Fact]
12+
public void ToHexString_Null()
13+
{
14+
var actual = HexConverter.ToString(null);
15+
Assert.Equal(string.Empty, actual);
16+
17+
actual = HexConverter.ToString([]);
18+
Assert.Equal(string.Empty, actual);
19+
}
20+
21+
[Fact]
22+
public void ToHexString_Exception()
23+
{
24+
var data = "1A021304FE1";
25+
var ex = Assert.ThrowsAny<ArgumentException>(() => HexConverter.ToBytes(data));
26+
Assert.NotNull(ex);
27+
}
28+
29+
[Fact]
30+
public void ToHexString_Ok()
31+
{
32+
var data = new byte[] { 0x1A, 0x02, 0x13, 0x04, 0xFE };
33+
var actual = HexConverter.ToString(data);
34+
Assert.Equal("1A-02-13-04-FE", actual);
35+
36+
actual = HexConverter.ToString(data, " ");
37+
Assert.Equal("1A 02 13 04 FE", actual);
38+
}
39+
40+
[Fact]
41+
public void ToBytes_Ok()
42+
{
43+
var excepted = new byte[] { 0x1A, 0x02, 0x13, 0x04, 0xFE };
44+
45+
var data = "1A021304FE";
46+
var actual = HexConverter.ToBytes(data);
47+
Assert.Equal(excepted, actual);
48+
49+
data = "1A-02-13-04-FE";
50+
actual = HexConverter.ToBytes(data, "-");
51+
Assert.Equal(excepted, actual);
52+
53+
data = "1A 02 13 04 FE";
54+
actual = HexConverter.ToBytes(data, " ");
55+
Assert.Equal(excepted, actual);
56+
}
57+
}

0 commit comments

Comments
 (0)