Skip to content
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

Add BitConverter Span-based APIs #22803

Closed
3 tasks done
stephentoub opened this issue Jul 18, 2017 · 6 comments
Closed
3 tasks done

Add BitConverter Span-based APIs #22803

stephentoub opened this issue Jul 18, 2017 · 6 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime
Milestone

Comments

@stephentoub
Copy link
Member

Separated out of https://github.com/dotnet/corefx/issues/21281 for tracking purposes.

  • Implement in System.Private.CoreLib in coreclr (it's "shared" with corert)
  • Expose from System.Runtime.Extensions contract in corefx
  • Add tests to System.Runtime.Extensions tests in corefx
namespace System
{
    public static class BitConverter
    {
        // Matching overloads for GetBytes. Copy to the destination span
        // rather than allocating a new byte[].  Return false if the destination
        // span isn't large enough, which can be determined before any copying.
        public static bool TryWriteBytes(Span<byte> destination, bool value);
        public static bool TryWriteBytes(Span<byte> destination, char value);
        public static bool TryWriteBytes(Span<byte> destination, double value);
        public static bool TryWriteBytes(Span<byte> destination, short value);
        public static bool TryWriteBytes(Span<byte> destination, int value);
        public static bool TryWriteBytes(Span<byte> destination, long value);
        public static bool TryWriteBytes(Span<byte> destination, float value);
        public static bool TryWriteBytes(Span<byte> destination, ushort value);
        public static bool TryWriteBytes(Span<byte> destination, uint value);
        public static bool TryWriteBytes(Span<byte> destination, ulong value);

        // Matching overloads for To*.  As with existing To* methods, throw if not
        // enough data in the source span.
        public static bool ToBoolean(ReadOnlySpan<byte> value);
        public static char ToChar(ReadOnlySpan<byte> value);
        public static double ToDouble(ReadOnlySpan<byte> value);
        public static short ToInt16(ReadOnlySpan<byte> value);
        public static int ToInt32(ReadOnlySpan<byte> value);
        public static long ToInt64(ReadOnlySpan<byte> value);
        public static float ToSingle(ReadOnlySpan<byte> value);
        public static ushort ToUInt16(ReadOnlySpan<byte> value);
        public static uint ToUInt32(ReadOnlySpan<byte> value);
        public static ulong ToUInt64(ReadOnlySpan<byte> value);}
}
@nchikanov
Copy link
Contributor

@stephentoub Since both my PRs are merged, is it safe to close this issue? :)

@stephentoub
Copy link
Member Author

Yup :)

@i3arnon
Copy link
Contributor

i3arnon commented Aug 19, 2017

What do you think about supporting different Endianness (e.g. ToInt32(buffer, Endianness.LittleEndian)?
It would be very useful when working with data coming off the network, as it's much less consistent than memory Endianness.

@stephentoub
Copy link
Member Author

@i3arnon, dotnet/corefx#22405

@i3arnon
Copy link
Contributor

i3arnon commented Aug 19, 2017

@stephentoub :). And I'm even subscribed to that issue.

@ghost
Copy link

ghost commented Nov 22, 2017

Would it be possible to have ToString(ReadOnlySpan<byte>) overload as well? If we have an ArraySegment and we want string representation, it will still not be possible without allocating an extra byte[] :(

@ghost ghost unassigned nchikanov Nov 22, 2017
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime
Projects
None yet
Development

No branches or pull requests

6 participants