1

I am having trouble decoding a number back into its character form. Each letter encoded using a base string of 96 letters

Example: To code "DCODE", written in base 26, ABCDEFGHIJKLMNOPQRSTUVWXYZ, convert it to base 10:
 D=3, C=2, O=14, D=3, E=4 so 
(3 x 26^4) + (2x26^2) + (14 x 26^1) + (4x 26^0) = 1415626

I understand how to encode it, but I'm confused on how we would go about decoding it? My base is 96 and not 26 but it's similar logic and I just found the above example online. We are given the size of the string, block_size, and the total amount, num.

alpha = """abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
 if num < len(alpha): 
        letter = alpha[num]
        text = ("a"*(block_size-1)) + letter

So far my cases only cover ones less than 96.: Similar to this except different bases: https://www.dcode.fr/base-26-cipher

Lizzy
  • 211
  • 2
  • 10
  • 1
    You have to first define/supply a cypher or a mapping between a character/codepoint and its numeric representation for all 96 value/letter pairs. – metatoaster Dec 11 '19 at 03:51
  • @metatoaster yep, added! – Lizzy Dec 11 '19 at 04:13
  • 2
    Looking around some more [here might be a generic solution](https://stackoverflow.com/questions/1119722/base-62-conversion). – metatoaster Dec 11 '19 at 04:17
  • Note that you can pre-calculate things like `base ** power`, and if you're smart, you see that you can even progressively calculate such values using multiplication in the loop... – Maarten Bodewes Dec 11 '19 at 14:08
  • That `dcode` site you point to continuously confuses cipher with a codec. A codec is not a cipher as it doesn't use a secret key. It doesn't produce ciphertext, it just produces a different encoding or representation of the same values. If I convert a number from base 10 to base 2, then I haven't *encrypted* it, right? – Maarten Bodewes Dec 11 '19 at 14:13
  • I'm not sure why you are given the size of the string and why it is called `block_size` by the way. Maybe you need to create a statically sized string? But that isn't a requirement for encoding / decoding. I can represent the number 100 in binary; it will take me 7 bits to do so (as it is in the range [0, 2^7) ). I don't need 7 to be specified up front. – Maarten Bodewes Dec 11 '19 at 14:22

0 Answers0