This translates the byte into an integer type:

byte[] array_of_bytes = new byte[]; Int16 integer_value = BitConverter.ToInt16(array_of_bytes, 0); 
  • Int16 - C #
  • Smallint - Pascal

Question: how to perform the same conversion of two bytes to mass into an integer value?

  • Reference type (with a house). tipidannih.narod.ru/ssilochnij.htm - nick_n_a
  • I think so p :^smallint; p=@array_of_bytes[0]; array_of_integers = p^; p :^smallint; p=@array_of_bytes[0]; array_of_integers = p^; How to get rid of p - declare a reference type. - nick_n_a

3 answers 3

 public static int[] GetIntArrayFromByteArray(byte[] byteArray) { int[] intArray = new int[byteArray.Length / 4]; for (int i = 0; i < byteArray.Length; i += 4) { intArray[i / 4] = BitConverter.ToInt32(byteArray, i); } return intArray; } 

    It is necessary to operate with reference types. http://tipidannih.narod.ru/ssilochnij.htm

    It is possible so, using reference types

      type PSmallint = ^Smallint; // обьявляем ссылочный тип var array_of_bytes : ^byte; array_of_integers : Smallint; // Аналог Int16 Begin array_of_bytes = GetMem(10 ); // указать ваш размер, или как-то получить. array_of_integers = PSmallint (@array_of_bytes[0])^; // Берём адрес, кастим к ссылочному типу, и домиком берём данные по адресу как int 16-битный end 
       type PSmallint = ^Smallint; // в новых версиях Delphi такой тип уже объявлен var BytesArray: array of byte; SmallIntValue: Smallint; begin SmallIntValue := PSmallint(@BytesArray[0])^; end; 

      It would be nice to start with, at a minimum, set the size of the array bytes Setlength(BytesArray, 2) , otherwise there will be an access error.

      • By the way, in case of a dynamic array (array of byte or tbytes) you can write simply: SmallIntValue := PSmallint(BytesArray)^; . - Alekcvp
      • @Alekcvp, I agree, but if we take the address of the zero element, then this will work anyway, and we will not need to remember / understand what kind of array we use. And the compiler optimizer will still generate the same code as the record from your comment. - kot-da-vinci