0
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 1 year has passed since last update.

半角英数のみのエンコードとデコードを basic32 パッケージで実現する

Posted at

こんにちは、皆さん!今回は Go 言語で半角英数のみの文字列をエンコードし、後にデコードする方法を紹介します。そのために、basic32 パッケージを使用します。

basic32 パッケージとは?

basic32 パッケージは、バイト列を base32 エンコーディングでエンコードおよびデコードするためのパッケージです。base32 エンコーディングは、半角英数のみの文字セットを使用してデータをエンコードする手法であり、主に文字列の安全な転送や保存に使用されます。

インストール

まず最初に、basic32 パッケージをインストールします。

go get github.com/savaki/basic32

エンコードとデコードの例

以下は、半角英数のみの文字列をエンコードし、後にデコードする Go のコード例です。

package main

import (
	"fmt"
	"github.com/savaki/basic32"
)

func main() {
	// エンコードする文字列
	input := "Hello123"

	// エンコード
	encoded := basic32.EncodeToString([]byte(input))
	fmt.Printf("Encoded: %s\n", encoded)

	// デコード
	decodedBytes, err := basic32.DecodeString(encoded)
	if err != nil {
		fmt.Printf("Error decoding: %v\n", err)
		return
	}
	decoded := string(decodedBytes)
	fmt.Printf("Decoded: %s\n", decoded)
}

このコードでは、まず basic32 パッケージをインポートし、指定された文字列をバイト列に変換してからエンコードとデコードを行っています。実行すると、元の文字列がエンコードされ、再びデコードされて元に戻ることが確認できます。

まとめ

今回は、半角英数のみの文字列を basic32 パッケージを使用してエンコードし、デコードする方法を紹介しました。この手法は、データの安全な転送や保存などのシナリオで役立つことがあります。ぜひ、お試しいただき、ご自身のプロジェクトに活用してみてください!

それでは、次回の記事でお会いしましょう!お楽しみに!

0
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
0
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?