-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataInt32LittleEndianConverter.cs
More file actions
33 lines (29 loc) · 1009 Bytes
/
DataInt32LittleEndianConverter.cs
File metadata and controls
33 lines (29 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using System.Buffers.Binary;
namespace BootstrapBlazor.Socket.DataConverters;
/// <summary>
/// Sokcet 数据转换为 int 数据小端转换器
/// </summary>
public class DataInt32LittleEndianConverter : IDataPropertyConverter
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="data"></param>
public object? Convert(ReadOnlyMemory<byte> data)
{
var ret = 0;
if (data.Length <= 4)
{
Span<byte> paddedSpan = stackalloc byte[4];
data.Span.CopyTo(paddedSpan[(4 - data.Length)..]);
if (BinaryPrimitives.TryReadInt32LittleEndian(paddedSpan, out var v))
{
ret = v;
}
}
return ret;
}
}