title: Encoding - What is this function?
body: <p>I'm porting and updating an old app and I came across this function. I'd like to know more about it, but I don't actually know what it's called. I'm assuming it has a popular name. Does anyone know?</p>
  
  <p>This version is in Python, although it was originally in Java.</p>
  
  <pre><code>def encode(msg): # msg is a string
  
    msg_len = len(msg)
    j = (msg_len + 6) / 7
    k = 0
  
    cbytesOutput = [ctypes.c_byte(0)]*(msg_len + j) # return is msg length + j bytes long
  
    for l in xrange(j):
      i1 = l * 8
      j1 = i1
      byte0 = ctypes.c_byte(-128)
      byte1 = ctypes.c_byte(1)
      k1 = 0
      while k1 &lt; 7 and k &lt; msg_len:
          byte2 = ctypes.c_byte(ord(msg[k]))
          if (byte2.value &amp; 0xffffff80) != 0:
              byte0 = ctypes.c_byte(byte0.value | byte1.value)
          j1 += 1
          cbytesOutput[j1] = ctypes.c_byte(byte2.value | 0xffffff80)
          byte1 = ctypes.c_byte(byte1.value &lt;&lt; 1)
          k += 1
          k1 += 1
  
      cbytesOutput[i1] = byte0
  
    return cbytesOutput
  </code></pre>
  
  <p>Any comments on the algorithm in general? I'm thinking of replacing it. Profiler says it's the worst function in the entire app (it's 60% of the time on the slowest code path) and it bloats the data as well.</p>
  
  <p>Thanks</p>