-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Memory
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 bytesConsumed, out int bytesWritten);
public static bool TryToHexString(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int bytesWritten);
public static bool TryToHexStringLower(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int bytesWritten);
}
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.
PaulusParssinen, MichalPetryka and 0xfeeddeadbeef
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Memory