Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I left pad a byte array efficiently

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?

like image 252
Null Reference Avatar asked Jul 30 '14 04:07

Null Reference


People also ask

How do you store bytes as strings?

The absolute safest way to convert bytes to a string and back is to use base64: string base64 = Convert. ToBase64String(bytes); byte[] bytes = Convert. FromBase64String(base64);

What is byte overflow?

Byte overflow is a problem that needs to be understood when dealing with bytes . If the result is greater than 127 or less than -128, then the byte variable overflows (i.e., it cannot contain the resulting value in a single byte). The remainder result is then displayed instead of the original result.


3 Answers

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.

like image 150
Random Dev Avatar answered Oct 04 '22 06:10

Random Dev


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.

like image 26
Enigmativity Avatar answered Oct 04 '22 06:10

Enigmativity


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

like image 27
Brannon Avatar answered Oct 04 '22 06:10

Brannon