Skip to content

Conversation

tannergooding
Copy link
Member

@tannergooding tannergooding commented Jul 23, 2025

This resolves #114079

Customer Impact

We exposed the FromHexString, ToHexString, and related APIs back in .NET 5 (2020) and have been getting requests to add UTF-8 support essentially as long. However, we didn't really make a more targeted effort to add native UTF-8 support until .NET 8 when we introduced IUtf8SpanParsable, IUtf8SpanFormattable, and ensured the core primitive numeric types all implemented these interfaces.

Exposing these as part of .NET 10 will unblock customers that have been waiting for this functionality and will give them the higher performance and quality they rely on from built-in APIs.

Testing

All the existing UTF-16 tests were updated to also test the UTF-8 paths.

Risk

Low. This is using the existing UTF-16 algorithms and simply updating them to work for byte. In most cases this is 1-to-1, in a couple places it uses the internal where TChar : IUtfChar<TChar> we use for other shared UTF-8/16 code paths.

@Copilot Copilot AI review requested due to automatic review settings July 23, 2025 02:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds UTF-8 support to the Convert class's hex string methods, exposing new overloads that work with ReadOnlySpan<byte> and Span<byte> for UTF-8 encoded hex strings. This complements the existing UTF-16 char-based methods and provides better performance for scenarios working with UTF-8 data.

Key changes:

  • Added UTF-8 overloads for FromHexString, TryToHexString, and TryToHexStringLower methods
  • Enhanced HexConverter with UTF-8 encoding/decoding support and vectorized optimizations
  • Updated tests to cover both UTF-16 and UTF-8 code paths comprehensively

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/libraries/System.Runtime/ref/System.Runtime.cs Added public API surface for new UTF-8 hex string methods
src/libraries/System.Private.CoreLib/src/System/Convert.cs Implemented UTF-8 hex string conversion methods and refactored existing code
src/libraries/Common/src/System/HexConverter.cs Added UTF-8 encoding/decoding support with vectorized implementations
src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs Added helper methods for hex-specific exceptions
src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf16Utility.cs Fixed vector comparison to use equality operator
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Convert.ToHexString.cs Added comprehensive UTF-8 test coverage for ToHexString methods
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Convert.FromHexString.cs Added comprehensive UTF-8 test coverage for FromHexString methods

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added minor question, LGTM otherwise.

@tarekgh tarekgh added this to the 10.0.0 milestone Jul 23, 2025
Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has my support for introducing in .NET 10 RC1

@tannergooding
Copy link
Member Author

This got M2 sign-off offline.

@tannergooding tannergooding merged commit c8d2430 into dotnet:main Jul 24, 2025
139 of 144 checks passed
@tannergooding tannergooding deleted the fix-114079 branch July 24, 2025 15:53
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Convert.{From/To}HexString(utf8)

3 participants