Skip to content

API proposal: BinaryPrimitives.ToLittleEndian / FromLittleEndian #29222

@GrabYourPitchforks

Description

@GrabYourPitchforks

There are some scenarios (see dotnet/coreclr#21948 (comment)) where we want to take a value in machine-endian order and convert it to little-endian. This is the opposite of normal APIs like htonl and ntohl, which convert between machine-endian and big-endian.

Proposed API additions

namespace System.Buffers.Binary
{
    // NEW methods added to EXISTING type
    public static class BinaryPrimitives
    {
        // convert from machine-endian to little-endian
        public static uint ToLittleEndian(uint value);

        // convert from little-endian to machine-endian
        public static uint FromLittleEndian(uint value);
    }
}

There would be overloads matching the existing overloads on ReverseEndianness: byte, sbyte, short, ushort, int, uint, long, ulong.

Open question: Do we also have ToBigEndian / FromBigEndian for parity and discoverability? Those APIs already exist on IPAddress.HostToNetworkOrder and IPAddress.NetworkToHostOrder. But those APIs require your application to pull in the package System.Net.Primitives, whereas these would all be contained within a single type.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions