Skip to content

Instantly share code, notes, and snippets.

@jerrylususu
Created April 27, 2023 15:00
Show Gist options
  • Save jerrylususu/fd9013b2be6543b188c745fcf431ae3b to your computer and use it in GitHub Desktop.
Save jerrylususu/fd9013b2be6543b188c745fcf431ae3b to your computer and use it in GitHub Desktop.
protobuf string to base64
<!DOCTYPE html>
<html>
<head>
<title>Base64 Encoder</title>
</head>
<body>
<textarea id="myTextArea" rows="10" cols="50"></textarea>
<br>
<button onclick="encodeText()">Encode to Base64</button>
<br>
<pre id="encodedText"></pre>
<script>
function encodeText() {
let text = document.getElementById("myTextArea").value;
console.log("text", text);
let rawValue = eval('"' + text + '"');
const result = [];
for (let i = 0; i < rawValue.length; i++) {
result.push(rawValue.charCodeAt(i));
}
const uint8array = new Uint8Array(result);
let base64Encoded = btoa(String.fromCharCode.apply(null, uint8array));
document.getElementById("encodedText").innerText = base64Encoded;
}
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "9ae31b1f",
"metadata": {},
"outputs": [],
"source": [
"encoded = \"ab\\272\\355\\367H\\275r\\305\\222\\374-*\\247\\233\\211\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "af1373a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abºí÷H½rÅ\\x92ü-*§\\x9b\\x89'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"encoded"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5465df43",
"metadata": {},
"outputs": [],
"source": [
"def convert(src):\n",
" return bytes([ord(i) for i in src])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2af11975",
"metadata": {},
"outputs": [],
"source": [
"b = convert(encoded)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "784adaaf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'6162baedf748bd72c592fc2d2aa79b89'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.hex()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "d73dba7e",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(b)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "eb2a10f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"97\n",
"98\n",
"186\n",
"237\n",
"247\n",
"72\n",
"189\n",
"114\n",
"197\n",
"146\n",
"252\n",
"45\n",
"42\n",
"167\n",
"155\n",
"137\n"
]
}
],
"source": [
"for i in b:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "89d89405",
"metadata": {},
"outputs": [],
"source": [
"import base64"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d525ba98",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'YWK67fdIvXLFkvwtKqebiQ=='\n"
]
}
],
"source": [
"print(base64.b64encode(b))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdd2e032",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "02d6044d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b4c141b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7fd57a33",
"metadata": {},
"outputs": [],
"source": [
"with open(\"a.txt\", \"r\") as f:\n",
" encoded_line = f.readline()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "c4995ad7",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"b'ab\\\\272\\\\355\\\\367H\\\\275r\\\\305\\\\222\\\\374-*\\\\247\\\\233\\\\211'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# can not be directly used (already escaped when used)\n",
"convert(encoded_line)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4289d265",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'YWJcMjcyXDM1NVwzNjdIXDI3NXJcMzA1XDIyMlwzNzQtKlwyNDdcMjMzXDIxMQ=='\n"
]
}
],
"source": [
"print(base64.b64encode(convert(encoded_line)))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "0b4f56d9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ab\\\\272\\\\355\\\\367H\\\\275r\\\\305\\\\222\\\\374-*\\\\247\\\\233\\\\211'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"encoded_line"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "99c7be7f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"'ab\\\\\\\\272\\\\\\\\355\\\\\\\\367H\\\\\\\\275r\\\\\\\\305\\\\\\\\222\\\\\\\\374-*\\\\\\\\247\\\\\\\\233\\\\\\\\211'\""
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"repr(encoded_line)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "8914ba46",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"'ab\\\\272\\\\355\\\\367H\\\\275r\\\\305\\\\222\\\\374-*\\\\247\\\\233\\\\211'\""
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# backslash\n",
"bs = \"\\\\\"\n",
"repr(encoded_line).replace(bs*2,bs)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "426338bc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abºí÷H½rÅ\\x92ü-*§\\x9b\\x89'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# now it works...\n",
"eval(repr(encoded_line).replace(bs*2,bs))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "3d75e67a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'6162baedf748bd72c592fc2d2aa79b89'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert(eval(repr(encoded_line).replace(bs*2,bs)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b6c164c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment