Skip to content

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

Open
@kzrnm

Description

@kzrnm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.MemoryuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions