최진용
최진용

Reputation: 43

Inverse Calculation

i want get t from encoded byte

Encode Code

byte enc(int t, int k)
{
    k %= 8;
    return (byte)(((t << k) | (t >> (8 - k))) & 0xff);
}

Tried

(65132 & 0xff) | ~0xff

Result

-148

Want

int dec(byte enc, int k)
    return t; // how to calculate t?

Upvotes: 0

Views: 75

Answers (1)

Kote
Kote

Reputation: 2256

If you want to write decode function, you need to know how your encode function works.
In this case it takes k right-most bytes and places its in tail.
E.g 1101 will be encoded to 0111.
So if you want to decode, you need to do opposite operations.

public static byte dec(byte enc, int k)
{
    k %= 8;
    return (byte)((enc >> k) | (enc << (8 - k)));
}

But if you are encoding your int to byte you may lose some information.
E.g you can't encode big enough number like 1234567890 to byte and then decode it.
But you can encode your int to array of bytes and then decode it from array.

Upvotes: 1

Related Questions