Skip to content

Instantly share code, notes, and snippets.

@xavierskip
Last active January 1, 2022 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavierskip/2a5d386ab58f59402327bdac42f31273 to your computer and use it in GitHub Desktop.
Save xavierskip/2a5d386ab58f59402327bdac42f31273 to your computer and use it in GitHub Desktop.
Cover your words, covert your string to 8-bit binary style
#!/usr/bin/env python2
# coding: utf-8
def cover(words, separator=' '):
return separator.join(['{:0>8}'.format(bin(ord(w))[2:]) for w in words])
def recover(words, separator=' '):
return ''.join([chr(int(b,2)) for b in words.split(separator)])
if __name__ == '__main__':
words = '你好世界,hello world'
bin_words = cover(words)
print(bin_words)
recover_words = recover(bin_words)
print(recover_words)
@xavierskip
Copy link
Author

xavierskip commented Jan 1, 2022

in python3

def recover(words, separator=' '):
    return  bytearray([ int(b,2) for b in words.split(separator) ]).decode('utf-8')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment