这是一种编码技术吗?

发布于 2024-12-19 17:27:55 字数 525 浏览 3 评论 0原文

ruakh的帖子在这里,我想确定以下代码片段是否属于 TLVKLV 编码类型:

interface

const
  Lex_0: string[length('(EOF)')] ='(EOF)';
  Lex_1: string[length('(Error)')] ='(Error)';

  LexTable : array[0..114] of ^String = (
  @Lex_0,
  @Lex_1
  )

Furhter to ruakh's post here, I want to ascertain wether the following snippet falls into the TLV or KLV encoding type :

interface

const
  Lex_0: string[length('(EOF)')] ='(EOF)';
  Lex_1: string[length('(Error)')] ='(Error)';

  LexTable : array[0..114] of ^String = (
  @Lex_0,
  @Lex_1
  )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-12-26 17:27:55

您的示例代码将无法工作,因为您正在混合 shortstring conststringpointer - 正如 Rob 在他的评论中所述。

并且您的代码与 KLV 或 TLV 编码无关。

这里可能是 TLV 编码的示例(我只显示字符串编码,但您可以添加其他类型):

type
  TTLVType = (tlvUnknown, tlvString, tlvInteger);

function EncodeToTLV(const aString: WideString): TBytes;
var Len: integer;
begin
  Len := length(aString)*2;
  SetLength(result,Len+sizeof(TTLVType)+sizeof(integer));
  Result[0] := ord(tlvString); // Type
  PInteger(@Result[sizeof(TTLVType)])^ := Len; // Length
  move(pointer(aString)^,Result[sizeof(TTLVType)+sizeof(integer)],Len); // Value
end;

function DecodeStringFromTLV(const aValue: TBytes): WideString;
begin
  if (length(aValue)<3) or (aValue[0]<>ord(tlvString)) or
     (PInteger(@aValue[sizeof(TTLVType)])^<>length(aValue)-sizeof(TTLVType)-sizeof(integer)) then
    raise EXception.Create('Invalid input format');
  SetString(result,PWideChar(@Result[sizeof(TTLVType)+sizeof(integer)]),PInteger(@aValue[sizeof(TTLVType)])^ div 2);
end;

我在这里使用 WideString 因为它可以安全地存储任何 Unicode 内容,即使在 Delphi 2009 之前的版本上也是如此编译器的。

您可以使用记录代替我的指针算术:

type
  TTLVHeader = packed record
    ContentType: TTLVType;
    ContentLength: integer;
    Content: array[0..1000] of byte; // but real length will vary
  end;
  PTLVHeader = ^TTLVHeader;

function EncodeToTLV(const aString: WideString): TBytes;
var Len: integer;
begin
  Len := length(aString)*2;
  SetLength(result,Len+sizeof(TTLVType)+sizeof(integer));
  with PTLVHeader(result)^ do 
  begin
    ContentType := tlvString;
    ContentLength := Len;
    move(pointer(aString)^,Content,Len); 
  end;  
end;

类似的编码可用于 KLV,但在标头中添加整数键。

Your sample code won't work, as you are mixing shortstring const and pointer of string - as Rob stated in his comment.

And your code has nothing to do with KLV nor TLV encoding.

Here may be a sample of TLV encoding (I show only string encoding, but you can add other types):

type
  TTLVType = (tlvUnknown, tlvString, tlvInteger);

function EncodeToTLV(const aString: WideString): TBytes;
var Len: integer;
begin
  Len := length(aString)*2;
  SetLength(result,Len+sizeof(TTLVType)+sizeof(integer));
  Result[0] := ord(tlvString); // Type
  PInteger(@Result[sizeof(TTLVType)])^ := Len; // Length
  move(pointer(aString)^,Result[sizeof(TTLVType)+sizeof(integer)],Len); // Value
end;

function DecodeStringFromTLV(const aValue: TBytes): WideString;
begin
  if (length(aValue)<3) or (aValue[0]<>ord(tlvString)) or
     (PInteger(@aValue[sizeof(TTLVType)])^<>length(aValue)-sizeof(TTLVType)-sizeof(integer)) then
    raise EXception.Create('Invalid input format');
  SetString(result,PWideChar(@Result[sizeof(TTLVType)+sizeof(integer)]),PInteger(@aValue[sizeof(TTLVType)])^ div 2);
end;

I used WideString here because it can safely store any Unicode content, even on pre-Delphi 2009 version of the compiler.

You may use a record instead of my pointer arithmetic:

type
  TTLVHeader = packed record
    ContentType: TTLVType;
    ContentLength: integer;
    Content: array[0..1000] of byte; // but real length will vary
  end;
  PTLVHeader = ^TTLVHeader;

function EncodeToTLV(const aString: WideString): TBytes;
var Len: integer;
begin
  Len := length(aString)*2;
  SetLength(result,Len+sizeof(TTLVType)+sizeof(integer));
  with PTLVHeader(result)^ do 
  begin
    ContentType := tlvString;
    ContentLength := Len;
    move(pointer(aString)^,Content,Len); 
  end;  
end;

A similar encoding may be used for KLV, but adding an integer key in the header.

捶死心动 2024-12-26 17:27:55

据我所知,这仅定义了两个常量(在本例中为 Lex_0Lex_1),并将它们放入名为 LexTable 的数组中,这根本不是编码。

Lex_0: string // declares a string constant by the name of Lex_0
[length('(EOF)')] // specifies the length of the string, this is equivalent to [5] in this case
='(EOF)' // sets the constant to '(EOF)'

然后创建指向字符串的 LexTable 数组,并将两个常量的地址放入该数组中。

As far as I can tell, this only defines two constants (Lex_0 and Lex_1 in this case) and puts them into an array called LexTable, this is no encoding at all.

Lex_0: string // declares a string constant by the name of Lex_0
[length('(EOF)')] // specifies the length of the string, this is equivalent to [5] in this case
='(EOF)' // sets the constant to '(EOF)'

then the LexTable array of pointers to string is created and addresses of the two constants are put in the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文