How do I left pad a byte array efficiently

8

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

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?

c#
.net
bytearray
padding
asked on Stack Overflow Jul 30, 2014 by Null Reference • edited Jul 30, 2014 by marc_s

3 Answers

11

I would recommend starting with Array.Copy like this:

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:

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.

answered on Stack Overflow Jul 30, 2014 by Carsten
7

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.

answered on Stack Overflow Jul 30, 2014 by Enigmativity
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

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).

answered on Stack Overflow Jul 30, 2014 by Brannon

User contributions licensed under CC BY-SA 3.0