user4593086
user4593086

Reputation:

How to Apply Reverse Logic for Decoding?

I'm on the last part of a decoding Python exercise, and this is really confusing me. The encoding represents the 376th to 65912th word with three chars: the first char is always (0xFA), the second is ((code - 376) // 256), and third is ((code - 376) % 256). For example, if the code is 30000, then the first output char would be 0xFA, the second 0x73, and the third 0xB8. (The code for 376 would be FA 00 00.)

Now here's my confusion, how can I interpret 0xfa 0x73 0xb8 as 30000? Because this word would be the 30000th word for my dictionary. Any help would be much appreciated, thanks.

Upvotes: 1

Views: 72

Answers (2)

pzp
pzp

Reputation: 6597

get_code = lambda c: int(c[1], 16)*256 + int(c[2], 16) + 376

chars = ('0xFA', '0x73', '0xB8')
print get_code(chars)

Upvotes: 1

SolaWing
SolaWing

Reputation: 1722

check the first char, if it is 0xFA, then

code = second * 256 + third + 376

Upvotes: 1

Related Questions