public static void WriteBase16String(this Utf8JsonWriter writer, string propertyName, ReadOnlySpan <byte> bytes)
        {
            writer.WritePropertyName(propertyName);

            byte[] rented = null;

            var         count = Base16.GetEncodedToUtf8Length(bytes.Length);
            Span <byte> utf8  = count <= 256 ? stackalloc byte[count] : (rented = System.Buffers.ArrayPool <byte> .Shared.Rent(count));

            try
            {
                var status = Base16.EncodeToUtf8(bytes, utf8, out var _, out var written);
                if (status != System.Buffers.OperationStatus.Done)
                {
                    throw new Exception("Unexpected failure in Base16 encode");
                }
                writer.WriteStringValue(utf8.Slice(0, written));
            }
            finally
            {
                if (rented != null)
                {
                    System.Buffers.ArrayPool <byte> .Shared.Return(rented);
                }
            }
        }
Exemple #2
0
        public override string ToString()
        {
            var bytes = UnsafeAsSpan(ref this);

            if (IsStandard)
            {
                var slice = bytes.Slice(12, 3);
                return(System.Text.Encoding.ASCII.GetString(slice));
            }
            else if (this == XRP)
            {
                return("XRP");
            }

            // Nonstandard Currency Code
            Span <byte> utf8 = stackalloc byte[Base16.GetEncodedToUtf8Length(bytes.Length)];
            var         _    = Base16.EncodeToUtf8(bytes, utf8, out var _, out var _);

            return(System.Text.Encoding.UTF8.GetString(utf8));
        }