Skip to content

Commit

Permalink
Merge pull request #302 from WeihanLi/patch-1
Browse files Browse the repository at this point in the history
Update HexStringExtension
  • Loading branch information
gmf520 authored Mar 6, 2025
2 parents 98a7037 + 539b785 commit 6e5e92e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/OSharp.Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,11 @@ public static string FromHexString(this string hexString, Encoding encoding = nu
/// <returns>十六进制字符串</returns>
public static string ToHexString(this byte[] bytes)
{
#if NET
return Convert.ToHexString(bytes);
#else
return bytes.Aggregate(string.Empty, (current, t) => current + t.ToString("X2"));
#endif
}

/// <summary>
Expand All @@ -753,12 +757,16 @@ public static byte[] ToHexBytes(this string hexString)
{
hexString = hexString ?? "";
hexString = hexString.Replace(" ", "");
#if NET
return Convert.FromHexString(hexString);
#else
byte[] bytes = new byte[hexString.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return bytes;
#endif
}

/// <summary>
Expand Down

0 comments on commit 6e5e92e

Please sign in to comment.