1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Base64 変換

Last updated at Posted at 2021-04-10

Base64 変換についてまとめてみました。

Python3 の例

encode01.py
#! /usr/bin/python
#
import base64
#
data_in = 'こんにちは'
bytes_in = data_in.encode()
#
encoded = base64.b64encode(bytes_in)
print(encoded)
decode01.py
#! /usr/bin/python
#
import base64
#
data = b'44GT44KT44Gr44Gh44Gv'
decoded = base64.b64decode(data)
str_out = decoded.decode()
print(str_out)
#

Node.js の例

encode01.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	encode01.js
//
//					Apr/10/2021
//
// ---------------------------------------------------------------
'use strict'

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const data_in = 'こんにちは'
const buffer = new Buffer.from(data_in)
const string = buffer.toString('base64')
console.log(string)

console.error ("*** 終了 ***")
// ---------------------------------------------------------------
decode01.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	decode01.js
//
//					Apr/10/2021
//
// ---------------------------------------------------------------
'use strict'

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const data_in = '44GT44KT44Gr44Gh44Gv'

const string = Buffer.from(data_in,'base64')
console.log(string.toString())

console.error ("*** 終了 ***")
// ---------------------------------------------------------------

Go の例

encode01.go
package main

import (
	b64 "encoding/base64"
	"fmt"
	"os"
)

func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	data := "こんにちは"

	sEnc := b64.StdEncoding.EncodeToString([]byte(data))
	fmt.Println(sEnc)
	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}
decode01.go
package main

import (
	b64 "encoding/base64"
	"fmt"
	"os"
)

func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	data_in := "44GT44KT44Gr44Gh44Gv"
	uDec, _ := b64.URLEncoding.DecodeString(data_in)
	fmt.Println(string(uDec))
	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

コマンドでの変換

エンコード

入力データ

a01.txt

こんにちは


>実行結果

>```text
$ base64 a01.txt
44GT44KT44Gr44Gh44Gv

デコード

入力データ

b01.txt

44GT44KT44Gr44Gh44Gv


>実行結果

>```text
$ base64 -d b01.txt
こんにちは
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?