获取数组类型的记录字段的长度 [英] Get length of record field of type array

查看:73
本文介绍了获取数组类型的记录字段的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写用于与外部二进制API通信的包装器。 API使用PDU(打包的二进制记录)进行通信。字符串是AnsiChar的数组,并且以零结尾:

I'm writing a wrapper for communication with an external binary API. The API uses PDUs (packed binary records) for communication. Strings are arrays of AnsiChar and are zero-terminated:

type 
  TSomePDU = packed record
    //...
    StringField: array[0..XYZ] of AnsiChar;
    //...
  end;
  PSomePDU = ^TSomePDU;

我想编写一个FillPDUString过程,该过程接受一个String并填充char数组,但是我想要为了避免在使用过程的任何地方跟踪MaxLength,因此我需要某种方式来获取声明的数组大小,并给出指向该字段的指针:

I want to write a FillPDUString procedure that would accept a String and fill the char array, but I want to avoid keeping track of MaxLength wherever the procedure is used, so I need somehow to get the declared array size given a pointer to the field:

function GetMaxSize(const Field: array of AnsiChar): Integer;
begin
  // ???
end;

//...
GetMaxSize(ARecord.StringField);

这可能吗?

推荐答案

如果我对您的理解正确,那么可以使用Delphi的长度函数

If I understand you correctly, then you can use Delphi's Length function

以下是获取长度的方法:

Here's how to get the length:

function GetMaxSize(const Value: PSomePDU): Integer; 
begin
  Result := Length(Value.StringField);
end;

这篇关于获取数组类型的记录字段的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆