C #: convert int to smallest byte array that it will put in

I would like an elegant, efficient tool to accept any unsigned integer and convert it to the smallest byte array into which it will fit. For instance:

250 = byte[1] 2000 = byte[2] 80000 = byte[3] 

so that I can write:

 var foo = getBytes(bar); 

and foo will have different lengths depending on the value of bar . How can I do it?

+6
source share
7 answers

You can do it as follows:

 public static byte[] ToByteArray(this int value) { var bytes = Enumerable .Range(0, sizeof(int)) .Select(index => index * 8) .Select(shift => (byte)((value >> shift) & 0x000000ff)) .Reverse() .SkipWhile(b => b == 0x00) .ToArray(); return bytes; } 

Then:

 int j = 2000; var bytes = j.ToByteArray(); Console.WriteLine(bytes.Length); for(int index = 0; index < bytes.Length; index++) { Console.WriteLine("{0:x}", bytes[index]); } 

gives:

 2 0x07 0xd0 

And when replacing j = 2000 with j = 80000 in the above example

 3 0x01 0x38 0x80 

And when replacing j = 2000 with j = 250 in the above example

 1 0xfa 
+2
source

There is no single method that you can use, but you can do it quite easily (warning - not verified)

 byte[] bytes; if ((i & 0xffffff00)==0) { bytes = new byte[] { (byte)i }; } else if ((i & 0xffff0000)==0) { bytes = new byte[] { (byte)(i & 0xff), (byte)((i & 0xff00) >> 8) }; } else if ((i & 0xff000000)==0) { bytes = new byte[] { (byte)(i & 0xff), (byte)((i & 0xff00) >> 8), (byte)((i & 0xff0000) >> 16) }; } else { bytes = BitConverter.GetBytes(i); } 
+2
source

This will give you all the bytes:

 static byte[] GetBytes(uint bar) { return BitConverter.GetBytes(bar).Reverse().SkipWhile(c => c == 0).Reverse().ToArray(); } 
+2
source

You can probably just hardcode the endpoints, but if you want a more dynamic approach, you can use something like this:

 byte[] getBytes(uint bar) { if (bar == 0) return new byte[0]; return getBytes(bar / 256) .Concat(Enumerable.Repeat((byte)(bar % 256), 1)) .ToArray(); } 
+1
source

It is also suitable for a long type (the only change is "byte" [4] "to" byte? [8] "and the declaration, of course).

 static byte[] ToArray(int num) { byte?[] b = new byte?[4]; int i = 0; do b[i++] = (byte)num; while ((num = num >> 8) > 0); byte[] result = new byte[i]; for (int j = 0; j < i; j++) result[j] = b[j].Value; return result; } 
+1
source
 private byte[] GetBytes(int number) { if (number < 0) { throw new ArgumentException("Can not be less than zero", "number"); } int numberOfBits = 0; while (number > 0) { numberOfBits++; number = number >> 1; } int reminder = 0; int numberofBytes = Math.DivRem(numberOfBits, 8, out reminder); numberofBytes = reminder > 0 ? numberofBytes + 1 : numberofBytes; return new byte[numberofBytes]; } 
0
source

It should be fast enough, and it allocates as much space as necessary.

 static byte[] getBytes(long a) { int k=0; for (long b=a; b!=0; b>>=8) k++; byte[] res = new byte[k]; for (k=0; a!=0; a>>=8) res[res.Length-(++k)] = (byte)(a&0xFF); return res; } 
0
source

Source: https://habr.com/ru/post/900941/


All Articles