将字节数组附加到包含'\0'的其他数组? [英] append byte array to an other array which contains '\0' ?

查看:100
本文介绍了将字节数组附加到包含'\0'的其他数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数,它将一个字节数组附加到另​​一个字节数组

但是当源包含'\0'时会出现问题

这是什么我有:



I have this function which append an array of byte to an other array of bytes
but the problem appears when the source contains '\0'
this what I have :

private void Aes_Strcat(sbyte[] a_pchResult, sbyte[] pchResult, int m_iBlockSize)
        {
            int x = 0;
            while (a_pchResult[x] != 0)
            {
                x++;
            }

            Buffer.BlockCopy(pchResult, 0, a_pchResult, x, m_iBlockSize);
        }





这里当a_pchResult包含例如{-10,15,3,45,17,25,0 ,53,51,12,0,0,0,0,0,0}

我想在最后添加pchResult(这意味着在'12'元素之后)

但正如你所说它会在'25'之后添加它,因为它在结束前找到'0'。



任何想法如何知道最后一个位置,如果它包含'\0'元素,则在源数组的末尾添加新数组。



here when the a_pchResult contains for example { -10, 15,3,45,17,25,0,56,41,12,0,0,0,0,0,0}
and I want to add pchResult to it at the end (it means after the '12' element)
but as you say it will add it after '25' because it find the '0' before the end.

any idea how know the last position to add the new array at the end of source array if it contains '\0' element.

推荐答案

字节数据通常不会被''终止' \''字符 - 这是一个完全正常且可接受的二进制值,范围为0到255(包括0和255)! \ 0根本不用作.NET数据中的终结符 - 它来自C和C ++世界。

通常,您将传递输出数组的已使用长度 - 如果你不这样做,你将覆盖错误的数据。
Byte data isn't normally terminated by a '\0' character - that's a perfectly normal, and acceptable binary value in the range 0 to 255 inclusive! '\0' is not used as a terminator in .NET data at all - it comes from the C and C++ world.
Normally, you would pass the "used" length of the output array - if you don't you will overwrite the wrong data.


我建​​议你创建一个包含数组的类,一个偏移计数器以及将它与另一个数组或实例合并的方法同一个班级。



请问是否有些事情不清楚 - 我的印象是你会知道我的意思,只是依旧坚持一些C心态: )



编辑:结果将类似于 MemoryStream [ ^ ]
I'd suggest you make a class containing the array, an offset-counter and methods for merging it with another array or instance of the same class.

Please ask if something shouldn't be clear - I have the impression you would know what I mean, just hanging on to some C mindset still :)

edit: The result would then be something similar to MemoryStream[^]


这只不过是滥用,大滥用。字符串和数组的终止方法首先是非常讨厌的。在体面的系统和语言,更现代和更精致的人,当完全远离这个明显的跛脚。数组和列表是自包含的对象,其中包含所有需要使用的对象,首先是它们的长度。如果使用数组,则使用一些长度对其进行初始化,并在数组对象本身中记住:

This would be nothing but abuse, big abuse. The "termination" approach on strings and arrays was really nasty in first place. In decent systems and languages, more modern and refined ones, people when away from this apparent lame completely. Arrays and list are self-contained objects which carry all one would need to work with then, first of all, their length. If you work with an array, you initialize it with some length, which is kept remembered in the array object itself:
int length = //...
sbyte[] array = new sbyte[length];

//...

for (int index = 0; index < array.Length; ++index) {
    // array.Length == length
}



类似的事情适用于排名较高的数组,不具有零基数的数组(参见 Array.GetLowerBound Array.GetUpperBound ),依此类推:

< a href =https://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx> https://msdn.microsoft.com/en-us/library /system.array%28v=vs.110%29.aspx [ ^ ],

HTTPS://msdn.microso ft.com/en-us/library/system.array.getlowerbound(v=vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.array.getupperbound (v = vs.110).aspx [ ^ ]。



-SA


Similar thing goes for arrays of higher rank, arrays with not zero-base indices (see Array.GetLowerBound, Array.GetUpperBound), and so on:
https://msdn.microsoft.com/en-us/library/system.array%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.array.getlowerbound(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.array.getupperbound(v=vs.110).aspx[^].

—SA


这篇关于将字节数组附加到包含'\0'的其他数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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