How and with what method can we write integer int в byte[]

Limited to 3.5, DataInputStream / DataOutputStream - not suitable.

Estimated code:

 // Записать этот инт int _sizeBlock = 10489; // В этот массив byte[] info = new byte[4]; 
  • one
    Well, you have an int number of 0х12345678 . What and where should be recorded? - VladD
  • I tried everything that caught my eye, had already lost the count, and so did not come to anything. - Garrus_En
  • 2
  • I do not understand. You can do it in a million different ways, the question is, what result do you expect ? - VladD
  • @ S.Kost, and what is not the answer? - Qwertiy

1 answer 1

 int _sizeBlock = 10489; byte[] info = BitConverter.GetBytes(_sizeBlock); 

method documentation

It is possible that the computer uses the reverse order for storing bytes. You need to check the BitConverter.IsLittleEndian flag if it is set to true "flip" bytes in the array.

 if (BitConverter.IsLittleEndian) Array.Reverse(info); 
  • The head was clogged yesterday at two in the morning. The solution floated on the surface. Thank you - Garrus_En
  • @Garrus_En, I was glad to help) - Gardes