파이썬 construct 라이브러리에서는 구문 분석중인 데이터에 플래그가 설정된 경우에만 의미가있는 필드가 있습니다.Python 구문 - 선택적 필드에 대한 데이터 사용
그러나 데이터 필드는 항상 존재합니다.
따라서 어떤 경우에도 데이터를 소비하지만 플래그 값에 따라 필드 값만 설정하고 싶습니다.
>>> struct.parse("010203".decode("hex"))
가
결과이어야 : 데이터에 대한
struct = Struct("struct",
Flag("flag"),
UBInt8("optional_data"),
UBInt8("mandatory")
)
: 구조 인 경우, 예를 들어
(부정확하게)으로 정의
Container({'flag': True, 'mandatory': 3, 'optional_data': 2})
및 데이터 :
>>> struct.parse("000203".decode("hex"))
원하는 결과는 다음
>>> struct.parse("000203".decode("hex"))
Container({'flag': False, 'mandatory': 3, 'optional_data': '\x02'})
:
는
struct = Struct("struct",
Flag("flag"),
IfThenElse("optional_data", lambda ctx: ctx.flag,
UBInt8("dummy"),
Padding(1)
),
UBInt8("mandatory")
)
그러나 패딩()과 같이, 필드의 원시 데이터를 둔다 :
Container({'flag': False, 'mandatory': 3, 'optional_data': None})
I는 다음 시도
감사합니다.