Open
Description
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.