Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First some notes</p> <ul> <li>If working with bits, use strings as a last resort</li> <li>Don't use <code>list</code> or <code>int</code> as variable names</li> </ul> <p>And finally, this isn't really an encoding as you can't ever get the original data back. Instead of doing this, a XOR cipher might better suit your needs.</p> <pre><code>def message_to_bits(msg): result = 0 for c in msg: result = (result &lt;&lt; 8) | ord(c) return result def get_bit(num, position): return num &amp; (1 &lt;&lt; position) def set_bit_on(num, position): return num | (1 &lt;&lt; position) def set_bit_off(num, position): return num &amp; ~(1 &lt;&lt; position) def set_bit(num, value, position): if value: return num | (1 &lt;&lt; position) else: return num &amp; ~(1 &lt;&lt; position) def encode(nested_list_thing, key): key_bits = message_to_bits(key) key_length = 8 * len(key) print ('key: {:0%db}' % key_length).format(key_bits) # Mask keeps track of which bit in # key_bits we are using to set the value of mask = 1 &lt;&lt; (key_length - 1) result = [] for list_ in nested_list_thing: encoded_list = [] for tuple_ in list_: encoded_tuple = [] for num in tuple_: # Encode the number set_to_bit = key_bits &amp; mask encoded_num = set_bit(num, set_to_bit, 0) encoded_tuple.append(encoded_num) # Move to next position in key_bits mask = mask &gt;&gt; 1 encoded_list.append(tuple(encoded_tuple)) result.append(encoded_list) return result image = [[(15, 103, 225), (0, 3, 19)], [(22, 200, 1), (8, 8, 8)], [(0, 0, 0), (5, 123, 19)]] key = 'hello' print encode(image, key) </code></pre> <p>This outputs</p> <pre><code>&gt; key: 0110100001100101011011000110110001101111 &gt; [[(14, 103, 225), (0, 3, 18)], [(22, 200, 0), (9, 9, 8)], [(0, 1, 0), (5, 122, 19)]] </code></pre> <p>EDIT: About decoding: If I gave you the encoded list and the key, how can you tell what the bits were before the encoding process? You can't. The key only tells you what the bits were set to, not what they were before you set them.</p> <p>That's why I recommended XOR ciphering, instead of setting the bits to a specific value, we flip some and leave others alone. If you don't have the key you don't know which bits were flipped and which were left. If you do have the key, you flip them again to get the original back.</p> <p>Try changing these lines in <code>encode</code></p> <pre><code> ... for num in tuple_: xor_key = 1 if key_bits &amp; mask else 0 encoded_num = num ^ xor_key encoded_tuple.append(encoded_num) # Move to next position in key_bits mask = mask &gt;&gt; 1 ... </code></pre> <p>then this at the bottom</p> <pre><code>image = [[(15, 103, 225), (0, 3, 19)], [(22, 200, 1), (8, 8, 8)], [(0, 0, 0), (5, 123, 19)]] key = 'hello' encoded_image = encode(image, key) decoded_image = encode(encoded_image, key) print encoded_image print decoded_image </code></pre> <p>It should give you</p> <pre><code>&gt; [[(15, 102, 224), (0, 2, 19)], [(22, 200, 1), (9, 9, 8)], [(0, 1, 0), (4, 123, 18)]] # &lt;-- encoded &gt; [[(15, 103, 225), (0, 3, 19)], [(22, 200, 1), (8, 8, 8)], [(0, 0, 0), (5, 123, 19)]] # &lt;-- decoded </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload