Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kzrnm opened this issue Mar 31, 2025 · 6 comments
Open

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

kzrnm opened this issue Mar 31, 2025 · 6 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory untriaged New issue has not been triaged by the area owner

Comments

@kzrnm
Copy link
Contributor

kzrnm commented Mar 31, 2025

Background and motivation

Recent .NET have introduced APIs like IUtf8SpanParsable<T> and IUtf8SpanFormattable for directly reading and writing UTF-8. #81500
It would be beneficial to have similar APIs for Hex string conversions in the Convert class as well.

API Proposal

namespace System;

public static class Convert
{
        public static byte[] FromHexString(ReadOnlySpan<byte> utf8Source);
        public static OperationStatus FromHexString(ReadOnlySpan<byte> utf8Source, Span<byte> destination, out int charsConsumed, out int bytesWritten);
        public static bool TryToHexString(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int charsWritten);
        public static bool TryToHexStringLower(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int charsWritten);
}

API Usage

var helloWorld = Convert.FromHexString("48656C6C6F2C576F726C64"u8);
Console.WriteLine(Encoding.UTF8.GetString(helloWorld));

Alternative Designs

No response

Risks

Users might get confused between the input ReadOnlySpan and the output Span.

@kzrnm kzrnm added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 31, 2025
@ghost ghost added the area-System.Memory label Mar 31, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 31, 2025
Copy link
Contributor

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

@kzrnm kzrnm changed the title [API Proposal]: Convert.*{Hex/Base64}* [API Proposal]: Convert.*{Hex/Base64}*(utf8) Mar 31, 2025
@Tornhoof
Copy link
Contributor

Tornhoof commented Mar 31, 2025

For base64 there is
https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64?view=net-9.0

That one should cover your utf8 Span needs.

@kzrnm kzrnm changed the title [API Proposal]: Convert.*{Hex/Base64}*(utf8) [API Proposal]: Convert.{From/To}HexString(utf8) Apr 1, 2025
@kzrnm
Copy link
Contributor Author

kzrnm commented Apr 1, 2025

@Tornhoof Hmm, I have narrow this proposal down to focus on Hex strings.

@huoyaoyuan
Copy link
Member

I'd like to get hex casing also included. Currently only upper case is supported in ToHexString.

@kzrnm
Copy link
Contributor Author

kzrnm commented Apr 1, 2025

@huoyaoyuan Wouldn't it already be supported by TryToHexStringLower or ToHexStringLower?

@huoyaoyuan
Copy link
Member

Wouldn't it already be supported by TryToHexStringLower or ToHexStringLower?

Just realized ToHexStringLower was included in 9.0. So the UTF-8 overloads should also include them, respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants