修改字节数组的最快方法是什么? [英] What is the fastest way to modify a byte array?

查看:72
本文介绍了修改字节数组的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改从内存中读取的字节数组..需要对其进行修改,然后再次将其全部重写到闪存中..但是我需要更快地进行操作..什么是最快的方法吗?............在此先感谢

我的代码:

byte []array1 = {3,4,2};
byte[] mybufferarry = ReadArrayFromFile();
int k = 0;
for(int i = 5 ; i < 3 ; i++)
    mybufferarray[i] = array1[k++];

解决方案

如何用您提供给我们的信息来回答这个问题?

编辑================

你的样品很烂.您指定的循环对mybuffarray无效,因为您将循环从5开始,直到循环为3,然后增加循环控制变量.


<如果您知道要更改的特定字节,则直接使用bytearray [x] = value进行设置.如果字节数组非常大,并且您不想遍历列表中的每个项目,则可以创建带有索引和新值的更新字典.然后,您只需要遍历字典即可,仅更改必要的字节.

根据您的代码段,您会遇到更大的问题.
您的for循环永远不会执行任何操作.您将i初始化为5,并且仅在小于3时才运行.这将如何工作?首先修复您的for循环,然后再次尝试调试.


可以使用内存映射文件 [ ^ ](.Net 4)?

如果不了解您的问题,就无法回答.我强烈怀疑(特别是由于您无法在示例中提供for循环)对内存中的字节数组进行直接索引访问应该足够快,特别是您遇到的任何速度问题都在代码的其他地方.


I need to modify a byte array that I''ve read from my memory.... a need to modify it an then rewrite to my flash all it again..but i need to do it faster.. what is the fastest way to do that?............ thanks in advance

My code:

byte []array1 = {3,4,2};
byte[] mybufferarry = ReadArrayFromFile();
int k = 0;
for(int i = 5 ; i < 3 ; i++)
    mybufferarray[i] = array1[k++];

解决方案

How can we possibly answer this with the information you''ve given us?

EDIT ================

Your sample sucks. The loop you specified won''t have any effect on mybuffarray because you''re starting the loop at 5, and going until the loop is 3, and INCREASING the loop control variable.


If you know the specific bytes you wish to change then just set them using bytearray[x] = value directly. If the byte array is very large and you do not want to iterate through each item in the list then you could create an update dictionary with an index and new value. Then you just have to iterate through the dictionary changing only the bytes necessary.

Based on your code snippet, you have bigger issues that you think.
There is no way that your for loop will ever execute anything. You initialize i to 5 and only run if it is less than 3. How is that going to work? Fix your for loop first and then try debugging again.


Can you use Memory Mapped Files[^] (.Net 4)?

Without knowing more about your problem it is impossible to answer. Direct indexed access to a byte array in memory should be fast enough, I strongly suspect (particularly given your inability to provide a working for loop in your sample) that any speed problems you are having are elsewhere in your code.


这篇关于修改字节数组的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆