tech_guy
tech_guy

Reputation: 13

how to append zero's to array which is already having a binary values

I have a decimal value of 126 which is converted to binary value using the below code:

binary[i] = Convert.ToString(bmparrayelement[i], 2);

then I got the value as "111 1110" which is right.

Then I want to append zeros along with this value in prefix that is "00 0111 1110"

Upvotes: 1

Views: 165

Answers (3)

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

Try this:

var binary = new byte[] {1, 1, 1, 1, 1, 1, 0};
var zeroed = new byte[] {0, 0};
binary = zeroed.Concat(binary).ToArray();

Update In .net 2.0 you can use:

        const int number = 2;
        var binary = new byte[] {1, 1, 1, 1, 1, 1, 0};

        var a = new byte[binary.Length + number];
        binary.CopyTo(a, number);
        binary = a;

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

Try something like this:-

 string s1 = Convert.ToString(byteArray[20], 2).PadLeft(10, '0');

Upvotes: 3

MVCKarl
MVCKarl

Reputation: 1295

Sorry if I have misunderstood but why can't you just do the below?

binary[i] = "00 0" + Convert.ToString(bmparrayelement[i], 2);

Upvotes: 2

Related Questions