Skip to content

Instantly share code, notes, and snippets.

@aaronsdevera
Created January 23, 2024 05:47
Show Gist options
  • Save aaronsdevera/0370309f601c32e81ec843f85435ab31 to your computer and use it in GitHub Desktop.
Save aaronsdevera/0370309f601c32e81ec843f85435ab31 to your computer and use it in GitHub Desktop.
base string utility in python
#! /usr/bin/python3
import sys
import os
INPUT = sys.argv[1]
RAW_STRING = None
if os.path.isfile(INPUT):
RAW_STRING = open(INPUT).read().strip()
else:
RAW_STRING = INPUT
def to_base3(n):
if n == 0:
return '0'
nums = []
while n:
n, r = divmod(n, 3)
nums.append(str(r))
return ''.join(reversed(nums))
def string_to_base3(s):
return ' '.join(to_base3(ord(c)) for c in s)
print(string_to_base3(RAW_STRING))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment