Skip to content

Instantly share code, notes, and snippets.

@ochaloup
Last active May 22, 2023 06:47
Show Gist options
  • Save ochaloup/e942f43e6c8a1356f422a1703596bad2 to your computer and use it in GitHub Desktop.
Save ochaloup/e942f43e6c8a1356f422a1703596bad2 to your computer and use it in GitHub Desktop.
Python script to a binary uint8 array (decimal numbers) to base64 formatted string
#!~/.pyenv/shims/python3
# Usage:
# tobase64.py [163,217,65,81,53,230,29,28,6,0,0,0,104,101,108,108,111,51]
# Output:
# o9lBUTXmHRwGAAAAaGVsbG8z
import base64
import sys
import json
if len(sys.argv) != 2:
print(f"Expecting one argument of json array listing uint items. The argument was not provided. Arguments: {sys.argv}")
exit(1)
json_string = sys.argv[1]
json_string = json_string.strip()
if not json_string.startswith("["):
print(f"Expecting the provided argument is a json array and starts with '[', but it is: {json_string}")
exit(2)
json_data = json.loads(json_string)
byte_array = bytearray(json_data)
print(base64.b64encode(byte_array).decode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment