# Length prefix in binary

• ``````class Codec:

def encode(self, strs):
"""Encodes a list of strings to a single string.

:type strs: List[str]
:rtype: str
"""
out = ''
for string in strs:
length = len(string)
binary = bin(length)[2:]
binary = '0'*(32-len(binary)) + binary
out    += binary + string

return out

def decode(self, s):
"""Decodes a single string to a list of strings.

:type s: str
:rtype: List[str]
"""
out = []
i   = 0
n   = len(s)
while i < n:
num = int(s[i:i+32],2)
out += s[i+32:i+32+num],
i   = i+32+num
return out
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(strs))
``````

• Why binary ?

• Basically the purpose is to do

``````<Length><Message>
``````

Now it Length could be of "Fixed" length (in my case it is 32 characters, a typical size for unsigned int). Or it could of "Variable" length - in which case I will need delimiter

``````<Length>:<Message><Length>:<Message>
``````

In the latter case,I will have to parse for regular expression or write my own code to read numeric characters and then find ":" and so on...":" could also be with in string and <number>":" pattern could also be possible within message -

``````([\d]+[:])
``````

So to make it a little simple I defined this format

``````"<length in binary of size 32><message>"
``````

It made logic a little simple (in my opinion) - at the cost of increased message size but if i were to actually pass it over wire - i will use it as sequence of bytes (length being 4 bytes) as opposed to sequence of characters (as demanded by API)

• Sorry if I was unclear. I meant why binary instead of the default representation, i.e., decimal (using fixed length 10). Doesn't binary just make both the result and the code longer? I don't see an advantage, only disadvantages.

• @ManuelP yes that's a better approach. Thank you for suggesting.

• but if i were to actually pass it over wire - i will use it as sequence of bytes (length being 4 bytes) as opposed to sequence of characters (as demanded by API)

Actually, the API here does ask for a sequence of bytes. Because that's what `str` is in Python 2, and "Python" at LeetCode means Python 2.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.