質問

furto to ruakh'の投稿 ここ, 、次のスニペットが TLV また KLV エンコードタイプ:

interface

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

  LexTable : array[0..114] of ^String = (
  @Lex_0,
  @Lex_1
  )
役に立ちましたか?

解決

ミキシングしているため、サンプルコードは機能しません shortstring constpointerstring - ロブが彼のコメントで述べたように。

そして、あなたのコードは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 ここでは、デルフィ以前のコンパイラバージョンでも、ユニコードコンテンツを安全に保存できるためです。

私のポインター算術の代わりにレコードを使用できます。

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に使用される場合がありますが、ヘッダーに整数キーを追加します。

他のヒント

私が知る限り、これは2つの定数のみを定義します(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 文字列へのポインターの配列が作成され、2つの定数のアドレスが配列に配置されます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top