如何有效地保留填充字节数组

时间:2022-11-25 14:08:47

Assuming I have an array

假设我有一个数组

LogoDataBy
{byte[0x00000008]}
    [0x00000000]: 0x41
    [0x00000001]: 0x42
    [0x00000002]: 0x43
    [0x00000003]: 0x44
    [0x00000004]: 0x31
    [0x00000005]: 0x32
    [0x00000006]: 0x33
    [0x00000007]: 0x34

I would like to create an array of an arbitrary length and left pad it with 0x00

我想创建一个任意长度的数组,并用0x00左边填充它

newArray
{byte[0x00000010]}
    [0x00000000]: 0x00
    [0x00000001]: 0x00
    [0x00000002]: 0x00
    [0x00000003]: 0x00
    [0x00000004]: 0x00
    [0x00000005]: 0x00
    [0x00000006]: 0x00
    [0x00000007]: 0x00
    [0x00000008]: 0x41
    [0x00000009]: 0x42
    [0x0000000a]: 0x43
    [0x0000000b]: 0x44
    [0x0000000c]: 0x31
    [0x0000000d]: 0x32
    [0x0000000e]: 0x33
    [0x0000000f]: 0x34

I have my current snippet here

我这里有我现在的片段

        string test = "ABCD1234";
        byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(test);
        var newArray = new byte[16];

        var difference = newArray.Length - LogoDataBy.Length;

        for (int i = 0; i < LogoDataBy.Length; i++)
        {
            newArray[difference + i] = LogoDataBy[i];
        }

Is there a more efficient way to do this?

有没有更有效的方法来做到这一点?

3 个解决方案

#1


7  

I would recommend starting with Array.Copy like this:

我建议从Array.Copy开始,如下所示:

string test = "ABCD1234";
byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(test);
var newArray = new byte[16];

var startAt = newArray.Length - LogoDataBy.Length;
Array.Copy(LogoDataBy, 0, newArray, startAt, LogoDataBy.Length);

If you really need the speed you can do Buffer.BlockCopy too:

如果你真的需要速度,你也可以做Buffer.BlockCopy:

string test = "ABCD1234";
byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(test);
var newArray = new byte[16];

var startAt = newArray.Length - LogoDataBy.Length;
Buffer.BlockCopy(LogoDataBy, 0, newArray, startAt, LogoDataBy.Length);

Note that I did not check the length of the array you provided - you should take care that it's big enough.

请注意,我没有检查您提供的阵列的长度 - 您应该注意它足够大。

#2


4  

Depending on how you define "more efficient" then this might be worth doing:

根据您如何定义“更高效”,这可能值得做:

var newArray =
    Enumerable
        .Repeat<Byte>(0, 16 - LogoDataBy.Length)
        .Concat(LogoDataBy)
        .ToArray();

This may not be computationally more efficient, but in terms of making the code clear and maintainable you might consider this am efficient way to code.

这可能在计算上不是更有效,但是在使代码清晰和可维护方面,您可能会认为这是一种有效的编码方式。

#3


1  

There are some other overloads of GetBytes that you can use. One of them allows you to specify a starting index in the array: http://msdn.microsoft.com/en-us/library/595a8te7%28v=vs.110%29.aspx

您可以使用其他一些GetBytes重载。其中一个允许您在数组中指定起始索引:http://msdn.microsoft.com/en-us/library/595a8te7%28v=vs.110%29.aspx

You can use the GetByteCount method on the encoding class to get the number of bytes that will exist after the encoding, although adding this additional call may negate any performance benefit. You may know that the byte count exactly matches the string length (depending upon your string source).

您可以在编码类上使用GetByteCount方法来获取编码后将存在的字节数,尽管添加此额外调用可能会抵消任何性能优势。您可能知道字节数与字符串长度完全匹配(取决于您的字符串源)。

#1


7  

I would recommend starting with Array.Copy like this:

我建议从Array.Copy开始,如下所示:

string test = "ABCD1234";
byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(test);
var newArray = new byte[16];

var startAt = newArray.Length - LogoDataBy.Length;
Array.Copy(LogoDataBy, 0, newArray, startAt, LogoDataBy.Length);

If you really need the speed you can do Buffer.BlockCopy too:

如果你真的需要速度,你也可以做Buffer.BlockCopy:

string test = "ABCD1234";
byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(test);
var newArray = new byte[16];

var startAt = newArray.Length - LogoDataBy.Length;
Buffer.BlockCopy(LogoDataBy, 0, newArray, startAt, LogoDataBy.Length);

Note that I did not check the length of the array you provided - you should take care that it's big enough.

请注意,我没有检查您提供的阵列的长度 - 您应该注意它足够大。

#2


4  

Depending on how you define "more efficient" then this might be worth doing:

根据您如何定义“更高效”,这可能值得做:

var newArray =
    Enumerable
        .Repeat<Byte>(0, 16 - LogoDataBy.Length)
        .Concat(LogoDataBy)
        .ToArray();

This may not be computationally more efficient, but in terms of making the code clear and maintainable you might consider this am efficient way to code.

这可能在计算上不是更有效,但是在使代码清晰和可维护方面,您可能会认为这是一种有效的编码方式。

#3


1  

There are some other overloads of GetBytes that you can use. One of them allows you to specify a starting index in the array: http://msdn.microsoft.com/en-us/library/595a8te7%28v=vs.110%29.aspx

您可以使用其他一些GetBytes重载。其中一个允许您在数组中指定起始索引:http://msdn.microsoft.com/en-us/library/595a8te7%28v=vs.110%29.aspx

You can use the GetByteCount method on the encoding class to get the number of bytes that will exist after the encoding, although adding this additional call may negate any performance benefit. You may know that the byte count exactly matches the string length (depending upon your string source).

您可以在编码类上使用GetByteCount方法来获取编码后将存在的字节数,尽管添加此额外调用可能会抵消任何性能优势。您可能知道字节数与字符串长度完全匹配(取决于您的字符串源)。