Browse Source

Byte[] 与 HexString 转换,去除空格和连字符。

master
王厅 1 day ago
parent
commit
655acb0bb4
  1. 5
      Apewer/BytesUtility.cs

5
Apewer/BytesUtility.cs

@ -165,7 +165,7 @@ namespace Apewer
#region Text #region Text
/// <summary>将字节数组转换为十六进制文本。</summary> /// <summary>将字节数组转换为十六进制文本(小写)。</summary>
public static string ToHex(this byte[] bytes) public static string ToHex(this byte[] bytes)
{ {
int length = bytes.Length; int length = bytes.Length;
@ -185,6 +185,9 @@ namespace Apewer
/// <summary>将十六进制文本转换为字节数组。</summary> /// <summary>将十六进制文本转换为字节数组。</summary>
public static byte[] FromHex(this string hex) public static byte[] FromHex(this string hex)
{ {
if (string.IsNullOrEmpty(hex)) return Empty;
hex = hex.Replace(" ", "").Replace("-", "");
if (string.IsNullOrEmpty(hex) || hex.Length < 2) return Empty; if (string.IsNullOrEmpty(hex) || hex.Length < 2) return Empty;
if (hex.Length % 2 != 0) return Empty; if (hex.Length % 2 != 0) return Empty;

Loading…
Cancel
Save