Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Binary
Strings
Encryption
Algorithms
Cryptography
Security

Description:

This is a string to binary and vice versa converter in python. The idea is that when the user enters a string into the function, a binary number for each character is returned.

e.g. hi --> 01101000 01101001

and 01101000 01101001 --> hi

Possible further applications could include something like containing code in a string and then executing it or just some fun encryption stuff.

Code
Diff
  • binary_converter = lambda s:"".join([chr(int(c,2))for c in s.split(" ")])if s[0]=="0"else" ".join([bin(ord(c))[2:].rjust(8,'0')for c in s])
  • 1-def binary_converter(string):
    2- result = ""
    3- if string[0] != '0':
    4- for character in string:
    5- result += str(bin(ord(character))[2:].zfill(8))
    6- # So characters can be told apart
    7- result += " " if character != string[len(string) - 1] else ""
    8- else:
    9- # This does the opposite, converting
    10- # the output of the above into the original input.
    11- store = [] # setting up list
    12- # Sorting numbers into individual items in the list
    13- for character in string:
    14- if character != ' ':
    15- result += character
    16- else:
    17- store.append(result)
    18- result = ""
    19- store.append(result)
    20- result = ""
    21-
    22- # getting results, aka the orginal string
    23- for item in store:
    24- result += chr(int(item, 2))
    25-
    26- print(result) # for debugging
    27- return result
    1+binary_converter = lambda s:"".join([chr(int(c,2))for c in s.split(" ")])if s[0]=="0"else" ".join([bin(ord(c))[2:].rjust(8,'0')for c in s])
Code
Diff
  • common_substring = lambda a, b: b in a
  • 1-def common_substring(string, sub_str):
    2- return sub_str in string
    1+common_substring = lambda a, b: b in a
Code
Diff
  • returnHundred=()=>100;
  • 1-function returnhundred(){return 'd'.charCodeAt();}
    1+returnHundred=()=>100;
Code
Diff
  • def find_only(values):
        return sorted(set(values), key=lambda x: values.count(x))[0]
  • 1-from collections import Counter
    2-def FindOnly(values):
    3- return Counter(values).most_common()[::-1][0][0]
    1+def find_only(values):
    2+ return sorted(set(values), key=lambda x: values.count(x))[0]
Code
Diff
  • reverseWords=s=>s.split(' ').reverse().join(' ');
  • 1-function reverseWords(str) {
    2- return str.split(" ").reverse().join(" ")
    3-}
    1+reverseWords=s=>s.split(' ').reverse().join(' ');
Code
Diff
  • from collections import Counter
    
    def FindOnly(values):
        items = Counter(values)
        for item in items:
            if items[item] == 1:
                return item
  • 1+from collections import Counter
    2+
    11 def FindOnly(values):
    2- for value in set(values):
    3- if 1 == values.count(value): # check value is unique
    4- return value
    4+ items = Counter(values)
    5+ for item in items:
    6+ if items[item] == 1:
    7+ return item
Code
Diff
  • function returnhundred() {
      return 'd'.charCodeAt();
    }
  • 11 function returnhundred() {
    2- return require('crypto').createHash('sha').update('Robson').digest()[0]
    2+ return 'd'.charCodeAt();
    33 }
Code
Diff
  • def decrypt(code, amount):
        return ''.join([chr((ord(c)-amount+65)%26+65) if c.isalpha() else c for c in code])
  • 1-def decrypt(code,amount):
    2- word = ""
    3- for i in code:
    4- if i==" ":
    5- word+=" "
    6- continue
    7- tmp = ord(i)-amount
    8- if tmp<65:
    9- tmp+=26
    10- word+=chr(tmp)
    11- return word
    1+def decrypt(code, amount):
    2+ return ''.join([chr((ord(c)-amount+65)%26+65) if c.isalpha() else c for c in code])