Do Delphi and FPC have MakeWord functions (two bytes or characters)?

These functions are useful:

function MakeWord(low, high: char): word;
begin
  result := byte(high) shl 8 + byte(low);
end; 

function MakeWord(low, high: byte): word;
begin
  result := high shl 8 + low;
end; 

      

However, I am wondering if such functionality exists (can be implemented in different ways) somewhere (not in a specific windows api file, but rather in a cross platform RTL function).

Usage example: Take two GT characters and enter the result WORD from it.

MakeWord('G','T');

      

I find this more readable than inserting SHL / SHR AND / OR hex operations and other hacks in the code that don't describe what the code does ... so surely these functions exist somewhere already? Or are people rolling back in their apps?

Or something like:

var x: word;
begin
  lo(x) := byte('G');
  hi(x) := byte('T');
end;

      

However, this won't work as the left side cannot be assigned: but it is more readable than some other solutions, IMO.

I think I've seen LoWord and HiWord (or LoByte, HiByte) functions, but usually in read-only cases. How about writing in hi and lo?

+3


source to share


2 answers


Delphi and FreePascal have a WordRec

block entry SysUtils

:

Delphi: http://docwiki.embarcadero.com/Libraries/en/System.SysUtils.WordRec

FreePascal: https://www.freepascal.org/docs-html/rtl/sysutils/wordrec.html



For example:

uses
 ..., SysUtils;

var
  x: Word;
begin
  WordRec(x).Lo := Byte(Ord('G'));
  WordRec(x).Hi := Byte(Ord('T'));
end;

      

+8


source


Yes, FreePascal has a makeword in the Windows box. (makeword is originally a winapi IIRC macro)



In addition, it also has built-in optimizations for late calls for small functions like this.

0


source







All Articles