English 中文(简体)
Convert IObservable<byte [], witherra periodic byte range to IObservable <byte [> 定期长度阵列
原标题:Convert IObservable<byte[]> with irregular length byte arrays to IObservable<byte[]> with regular length arrays

I have an IObservable<byte[]> that gives me an uncertain amount of bytes in the byte array. I want to know how I go from that, to returning an IObservable<byte[]> with a set amount of bytes in each byte array. Let s assume we want 10 bytes at a time.

That is to say, if I get the following input if I were to subscribe:

{1, 2, 3, 4}
{5, 6}
{7, 8, 9}
{10}
{11, 12, 13, 14, 15}
{16}
{17, 18}
{19, 20}

Bytes.Subscribe(b => Console.WriteLine(b.Length));

产出将是

3
2
3
1
5
1
2
2

我想将上述意见转化为:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

Bytes.<WhateverItTakesToDoThat>.Subscribe(b => Console.WriteLine(b.Length));

产出将是

10
10

如果某一批的矿石数量大于单一输出包装,即:

{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
{33, 34, 35, 36, 37, 38, 39, 40, 41}
{42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52}

应变成

{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50}

(待补)

最佳回答

它很容易。 为此:

    Bytes
        .SelectMany(b => b)
        .Buffer(10)
        .Select(bs => bs.ToArray());
问题回答

我在经过一线思考和细化之后,找到了一个解决办法。 下面的法典是我想要的:

Bytes.Select( b => b.ToObservable() ) // Convert input to IObservable<IObservable<byte>>
.Merge( 1 ) // Merges the IObservable<IObservable<byte>> to an IObservable<byte>
            // with the bytes in the right order
.Buffer( 4 ) // Wait until we have 4 bytes ready
.Select( bl => bl.ToArray() ) // Take these 4 bytes and turn them back into an array
.Subscribe( b => Console.WriteLine( b.Length ) );

这可能是效率低下的,我几乎肯定,这不会给他最有效的方式,这样,如果在座的一些人能够找到更好、更有效的解决办法,我就一耳不闻!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签