Tell me which type to use for a 3 byte number? Or if not how to make your type of number. You need exactly 3 bytes. Tk I read the structure of such a file and describe the entire structure in the record. I want the numbers to go straight to the variables without too much difficulty. How to be?

  • An array of three bytes does not suit you? - Vladimir Martyanov
  • one
    And in connection with which the need for such torment, instead of using the usual integer or longint? The processor processes operands of 4 bytes each; therefore, 3 byte data types most likely do not exist in nature. You go against the architecture of the equipment. Either you have to write your functions for adding / multiplying the division of three-byte arrays - Albert Fomin
  • one
    And what you do not like 4-byte type? - Aquinary
  • 2
    It is better to use 4-byte structures for processing, that is, an integer, and when reading and writing to convert them, it may be a question of saving space for very large amounts of data. In any other case, the code will be slower or unnecessarily more complex. In Delphi, alignment (Project option - record field alignment) is specially designed to speed up the code. - Albert Fomin
  • I can not change the structure that writes the equipment XXX, because This machine was not created by me. Yes, I get an array of 3 bytes, well, how can I convert them into a 4-byte integer, I understand it will work, but for the beauty of the structure I would like 3, if not, how to convert these 3 bytes into an integer? Although this is not an integer, because the score goes from zero, without minuses, it is 3 bytes, the timestamp in seconds - Alex Lizenberg

2 answers 2

Taking into account

Tk I read the structure of this file

I think that you use TFileStream or another TStream heir for this.

I do not remember which regular functions are allowed in D7, so in the examples I use those that are 100%.

type T3Bytes = array[0..2] of integer; // судя по комментариям - эта процедура вами не будет использоваться // поэтому ее вызовы я закомментировал. procedure Transform3SubZero(var Value: integer); begin if (Value and $800000)<>0 then // если старший бит установлен Value:=Value or $FF000000); // то число отрицательное end; // если младший байт - впереди function Transform3BytesToIntLE(Bytes: T3Bytes): integer; begin Result:=0; // обнуляем старший байт Move(Bytes[0], Result, 3); // копируем в Integer //Transform3SubZero(Result); // если числа могут быть отрицательными end; // если старший байт - впереди function Transform3BytesToIntBE(Bytes: T3Bytes): integer; var tmp: Byte; begin // меняем порядок байт tmp:=Bytes[0]; Bytes[0]:=Bytes[2]; Bytes[2]:=tmp; Result:=Transform3BytesToIntLE(Bytes); end; function Load3BytesIntFromStreamLE(Stream: TStream): integer; var Bytes: T3Bytes; begin Stream.Read(Bytes[0], 3); Result:=Transform3BytesToIntLE(Bytes); end; function Load3BytesIntFromStreamBE(Stream: TStream): integer; var Bytes: T3Bytes; begin Stream.Read(Bytes[0], 3); Result:=Transform3BytesToIntBE(Bytes); end; 

it is better to keep the structure not in the record, since D7 does not allow using functions in them, but in classes, it will be easier to read data:

 TmyStruct = class public Field1: integer; Field2: integer; procedure LoadFromStream(Stream: TStream); end; procedure TmyStruct.LoadFromStream(Stream: TStream); begin Field1:=Load3BytesIntFromStreamLE(Stream); Field2:=Load3BytesIntFromStreamLE(Stream); end; 

    Use an array of 3 bytes

     T3Bytes = array[0..2] of Byte 

    or record

     T3Bytes = packed record x: Byte; y: Byte; z: Byte; end; 
    • Specify how to use it for the case of topikstarter? - Kromster