字节数组用字节序列替换字节效率:迭代和复制与 SelectMany
我正在处理一个包含文本消息的字节数组,但消息中的某些字符是控制字符(即小于 0x20),我想将它们替换为解码为 ASCII 时人类可读的字符序列(例如 0x0F
将显示 [TAB]
而不是实际的制表符)。在我看来,我有三个选择:
- 将整个内容解码为 ASCII 字符串,然后使用
String.Replace()
交换出我想要的内容。这样做的问题是,这些字符似乎只是被解码为不可打印的框字符或问号,从而丢失了它们的实际字节值。 - 遍历字节数组,查找任何控制字符并执行数组插入操作(创建新的更大数组,复制现有片段,写入新片段)。
- 使用 Array.ToList
() 将字节数组转换为 List
,然后使用IEnumerable.SelectMany()
转换控件将字符转换为可读字符序列,然后SelectMany
将为我展平回来。
那么问题来了,就效率而言,哪个是最佳选择?我对 IEnumerable lambda 操作的性能影响并没有很好的感觉。我相信选项 1 在功能上不可行,但我可能是错的。
I'm dealing with a byte array that comprises a text message, but some of the characters in the message are control characters (i.e. less than 0x20) and I want to replace them with sequences of characters that are human readable when decoded into ASCII (for instance 0x0F
would display [TAB]
instead of actually being a tab character). So as I see it, I have three options:
- Decode the whole thing into an ASCII string, then use
String.Replace()
to swap out what I want. The problem with this is that the characters seem to just be decoded as the unprintable box character or question marks, thus losing their actual byte values. - Iterate through the byte array looking for any of my control characters and performing an array insert operation (make new larger array, copy existing pieces in, write new pieces).
- Use
Array.ToList<byte>()
to convert the byte array to aList
, then useIEnumerable.SelectMany()
to transform the control characters into sequences of readable characters whichSelectMany
will then flatten back out for me.
So the question is, which is the best option in terms of efficiency? I don't really have a good feel for the performance implications of the IEnumerable
lambda operations. I believe option 1 is out as functionally unworkable, but I could be wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
编辑:
替换 8 位字节值的示例
关于性能
我不是 100% 确定这是否是最快的方法...我们只是尝试了几种方法,尽管我们的要求是替换原始字节数组中的“1-n 字节的字节数组”...这来自 fastet+对于我们的用例来说是最干净的(1 MB - 1 GB 文件)。
Try
EDIT:
Sample for replacing an 8 Bit byte value
Regarding the performance
I am not 100% sure whether it is the fastest approach... we just tried several approaches though our requirement was to replace "byte array of 1-n bytes" whithin the original byte-array... this came out the fastet+cleanest for our use case (1 MB - 1 GB files).