1

i have exponent whats 3 bytes long. Now i need it to be 4 bytes. I found somewhere that i could pad at the leading octets.. but i have no idea to do that.. So can anybody help me out?

Example Input: exponent what i have right now is 65537, int bytes its then 01 00 01.

hs2d
  • 6,027
  • 24
  • 64
  • 103

2 Answers2

3

Assuming you just want to pad it with zeroes, create a new four byte array and copy the existing one into it:

byte[] newArray = new byte[4];
// Copy bytes 0, 1, 2 of oldArray into 1, 2, 3 of newArray.
Array.Copy(oldArray, 0, newArray, 1, 3);

(You can do this manually with three assignments as well; that's potentially simpler for this situation, but doesn't scale well (in terms of code) to larger sizes.)

Change the "1" to "0" if you find you need the padding at the end instead of the start... or use Array.Resize in that case.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Not entirely sure of the meaning, but it sounds like you just want:

byte[] first = /* 3 bytes */
byte[] second = new byte[4];
// since 3 bytes, we'll do this manually; note second[0] is 0 already
second[1] = first[0];
second[2] = first[1];
second[3] = first[2];

Of course, if you are actually dealing with an int it is already padded on the left with 0, to 4 bytes.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900