等效于StringBuilder的字节数组

等效于StringBuilder的字节数组

本文介绍了等效于StringBuilder的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的答案,我认为应该可以回答。我确实尝试在此处找到答案,但没有提出任何建议-如果您错过了我想念的东西,我们深表歉意。

This is a simple one, and one that I thought would have been answered. I did try to find an answer on here, but didn't come up with anything - so apologies if there is something I have missed.

我不为 Append()的所有不同重载而烦恼-但我想查看 Append(byte) Append(byte [])

I'm not bothered about all the different overloads of Append() - but I'd like to see Append(byte) and Append(byte[]).

周围有什么东西吗?

推荐答案

会为您工作?该接口可能不太方便,但是它提供了一种附加字节的简单方法,完成后,您可以通过调用< byte [] 来获取内容。 a href = http://msdn.microsoft.com/zh-cn/library/system.io.memorystream.toarray.aspx rel = noreferrer> ToArray()

Would MemoryStream work for you? The interface might not be quite as convenient, but it offers a simple way to append bytes, and when you are done you can get the contents as a byte[] by calling ToArray().

更多的类似于 StringBuilder 的接口可以通过使用扩展方法来实现。

A more StringBuilder-like interface could probably be achieved by making an extension method.

更新

扩展方法如下:

Update
Extension methods could look like this:

public static class MemoryStreamExtensions
{
    public static void Append(this MemoryStream stream, byte value)
    {
        stream.Append(new[] { value });
    }

    public static void Append(this MemoryStream stream, byte[] values)
    {
        stream.Write(values, 0, values.Length);
    }
}

用法:

MemoryStream stream = new MemoryStream();
stream.Append(67);
stream.Append(new byte[] { 68, 69 });
byte[] data = stream.ToArray();  // gets an array with bytes 67, 68 and 69

这篇关于等效于StringBuilder的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:59