首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解码ULEB128

解码ULEB128
EN

Stack Overflow用户
提问于 2017-01-27 19:33:10
回答 1查看 674关注 0票数 1

伪码:

代码语言:javascript
复制
function DecodeULEB128(var AULEB128: UInt64): UInt64;
var
  lShift : Cardinal;
  lByte : Byte;
begin
  Result := 0;
  lShift = 0;

  while true do
  begin
    lByte := next byte of AULEB128;
    Result = Result or (low order 7 bits of lByte << lShift);
    if (high order bit of lByte = 0) then
       break;
    Inc(lShift, 7);
  end;
end;

我有两个问题:

1)如何访问AULEB128的字节,以便将其中的一个分配给lByte?

2)如何访问Byte的低阶/高阶位?

我知道如何使用SysUtils中定义的记录来访问低阶/高阶位:

代码语言:javascript
复制
  WordRec = packed record
    case Integer of
      0: (Lo, Hi: Byte);
      1: (Bytes: array [0..1] of Byte);
  end;

  LongRec = packed record
    case Integer of
      0: (Lo, Hi: Word);
      1: (Words: array [0..1] of Word);
      2: (Bytes: array [0..3] of Byte);
  end;

  Int64Rec = packed record
    case Integer of
      0: (Lo, Hi: Cardinal);
      1: (Cardinals: array [0..1] of Cardinal);
      2: (Words: array [0..3] of Word);
      3: (Bytes: array [0..7] of Byte);
  end;

我习惯于这样做: Int64Rec(Var).Lo。

我是否必须使用字节上的and运算符来访问一些?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-27 22:21:19

试着做这样的事情:

代码语言:javascript
复制
function DecodeULEB128(AULEB128: UInt64): UInt64;
var
  I, lShift : Integer;
  lByte : Byte;
begin
  Result := 0;
  lShift := 0;
  for I := 0 to 7 do
  // or maybe 'for I := 7 downto 0' instead? 
  // Not sure which direction you should be looping...
  begin
    lByte := Int64Rec(AULEB128).Bytes[I];
    Result := Result or (UInt64(lByte and $7F) shl lShift);
    if (lByte and $80) = 0 then
       Break;
    Inc(lShift, 7);
  end;
end;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41901668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档