C头到delphi

XBa*_*000 2 c delphi

我有一段C代码.我需要帮助才能将其翻译成Delphi代码.

1)

/*
 * Color is packed into 16-bit word as follows:
 *
 *  15      8 7      0 
 *   XXggbbbb XXrrrrgg
 *
 * Note that green bits 12 and 13 are the lower bits of green component
 * and bits 0 and 1 are the higher ones.
 * 
 */
#define CLR_RED(spec)      (((spec) >> 2) & 0x0F)
#define CLR_GREEN(spec)    ((((spec) & 0x03) << 2) | ((spec & 0x3000) >> 12))
#define CLR_BLUE(spec)     (((spec) >> 8) & 0x0F)
Run Code Online (Sandbox Code Playgroud)

2)

#define CDG_GET_SCROLL_COMMAND(scroll)    (((scroll) & 0x30) >> 4)
#define CDG_GET_SCROLL_HOFFSET(scroll)     ((scroll) & 0x07)
#define CDG_GET_SCROLL_VOFFSET(scroll)     ((scroll) & 0x0F)
Run Code Online (Sandbox Code Playgroud)

Cod*_*aos 11

这些是参数化宏.由于Delphi不支持这些,所以你需要使用函数,无论如何它都是更干净的.

  • >>shr德尔福是一个右移
  • <<shl德尔福是一个左移
  • &是"按位和",and在德尔福
    德尔福整数工作时,和布尔工作时的逻辑运算符,所以只有一个操作员使用位运算符and来代替这两个&&&.
  • |or在Delphi中是"按位或"
  • 0x$Delphi中十六进制文字的前缀

所以#define CLR_GREEN(spec) ((((spec) & 0x03) << 2) | ((spec & 0x3000) >> 12))变得像:

function CLR_GREEN(spec: word):byte;
begin
  result := byte(((spec and $03) shl 2) or ((spec and $3000) shr 12));
end;
Run Code Online (Sandbox Code Playgroud)

(我手头没有delphi,所以可能存在小错误)

以类似的方式转换其他宏.