Skip to content

Instantly share code, notes, and snippets.

@CookieBox26
Created November 7, 2021 23:45
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 CookieBox26/9ea7b623c7f47d115935524bdaa7ea2b to your computer and use it in GitHub Desktop.
Save CookieBox26/9ea7b623c7f47d115935524bdaa7ea2b to your computer and use it in GitHub Desktop.
バイト列を Base64 文字列に変換
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# バイト列を Base64 文字列に変換\n",
"\n",
"参考文献: [base64ってなんぞ??理解のために実装してみた - Qiita](https://qiita.com/PlanetMeron/items/2905e2d0aa7fe46a36d4)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"YWJjZGVmZw==\n",
"RGFua2VzY2jDtm4=\n",
"44GC44KK44GM44Go44GG\n"
]
}
],
"source": [
"mapping = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'\n",
"\n",
"def byteseq_to_base64str(byteseq):\n",
" # 先頭のバイトから順に 8 桁の 2 進数表記の文字列にしていく\n",
" binstr = ''\n",
" for b in byteseq:\n",
" binstr += format(b, 'b').zfill(8)\n",
" # 文字数が 6 の倍数でないときパディング\n",
" rem = len(binstr) % 6\n",
" if rem != 0:\n",
" binstr += '0' * (6 - rem)\n",
" # 6 文字ずつ整数として取り出しマッピング\n",
" b64str = ''.join([mapping[int(binstr[i: i+6], 2)] for i in range(0, len(binstr), 6)])\n",
" # 文字数が 4 の倍数でないときパディング\n",
" rem = len(b64str) % 4\n",
" if rem != 0:\n",
" b64str += '=' * (4 - rem)\n",
" return b64str\n",
"\n",
"# 文字列を utf-8 エンコーディングでバイト列にしたもので確認\n",
"print(byteseq_to_base64str('abcdefg'.encode()))\n",
"print(byteseq_to_base64str('Dankeschön'.encode()))\n",
"print(byteseq_to_base64str('ありがとう'.encode()))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'YWJjZGVmZw=='\n",
"b'RGFua2VzY2jDtm4='\n",
"b'44GC44KK44GM44Go44GG'\n"
]
}
],
"source": [
"import base64\n",
"print(base64.b64encode('abcdefg'.encode()))\n",
"print(base64.b64encode('Dankeschön'.encode()))\n",
"print(base64.b64encode('ありがとう'.encode()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@CookieBox26
Copy link
Author

以下のようにかく方がもっとコンパクトである。
前者はどこまでも大きい整数を扱うことになる。もっとも Python3 はどこまでも大きい整数を扱えるらしい。

mapping = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

def byteseq_to_base64str(byteseq):
    binstr = format(int(byteseq.hex(), 16), 'b')
    binstr = '0' * ((8 - len(binstr) % 8) % 8) + binstr
    binstr = binstr + '0' * ((6 - len(binstr) % 6) % 6)
    b64str = ''.join([mapping[int(binstr[i: i+6], 2)] for i in range(0, len(binstr), 6)])
    return b64str + '=' * ((4 - len(b64str) % 4) % 4)

def byteseq_to_base64str(byteseq):
    binstr = ''
    for b in byteseq:
        binstr += format(b, 'b').zfill(8)
    binstr = binstr + '0' * ((6 - len(binstr) % 6) % 6)
    b64str = ''.join([mapping[int(binstr[i: i+6], 2)] for i in range(0, len(binstr), 6)])
    return b64str + '=' * ((4 - len(b64str) % 4) % 4)

# 文字列を utf-8 エンコーディングでバイト列にしたもので確認
print(byteseq_to_base64str('abcdefg'.encode()))
print(byteseq_to_base64str('Dankeschön'.encode()))
print(byteseq_to_base64str('ありがとう'.encode()))

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