C# サンプル集 |
Visual Basic 中学校 > C# サンプル集 > C# サンプル集目次 >
Base64Urlにエンコードする
2023/1/1
→ Visual Basic のサンプルに切り替える → Python のサンプルに切り替える
目次
文字列をUTF-8としてBase64Urlにエンコードする
string value = "徳川家康ABC";
string base64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(value));
base64 = base64.Replace("+", "-").Replace("/", "_").Replace("=", "");
System.Diagnostics.Debug.WriteLine(base64); //5b6z5bed5a625bq3QUJD
これでもできます。
string value = "徳川家康ABC";
string base64 = Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Encode(System.Text.Encoding.UTF8.GetBytes(value));
System.Diagnostics.Debug.WriteLine(base64); //5b6z5bed5a625bq3QUJD
メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法
文字列をShift_JISとしてBase64Urlにエンコードする
#if NETCOREAPP //.NET CoreでShift_JISなど追加のエンコーディングを扱うために必要です。 System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); #endif string value = "徳川家康ABC"; string base64 = Convert.ToBase64String(System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(value)); base64 = base64.Replace("+", "-").Replace("/", "_").Replace("=", ""); System.Diagnostics.Debug.WriteLine(base64); /
/k7-Q7InGjU5BQkM
これでもできます。
#if NETCOREAPP
//.NET CoreでShift_JISなど追加のエンコーディングを扱うために必要です。
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
string value = "徳川家康ABC";
string base64 = Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Encode(System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(value));
System.Diagnostics.Debug.WriteLine(base64); //k7-Q7InGjU5BQkM
メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法
オブジェクトをBase64Urlにエンコードする
オブジェクトの一例として、List<string> をBase64Urlにする例。
var values = new List<string> { "Apple","徳川家康","12345" };
string json = System.Text.Json.JsonSerializer.Serialize(values);
string base64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(json));
base64 = base64.Replace("+", "-").Replace("/", "_").Replace("=", "");
//WyJBcHBsZSIsIlx1NUZCM1x1NURERFx1NUJCNlx1NUVCNyIsIjEyMzQ1Il0
System.Diagnostics.Debug.WriteLine(base64);
メモ:この例ではオブジェクトをJSON形式で文字列化したものをBase64Urlにしています。
メモ:List<string>に限らず、JsonSerializerでJSON化できるオブジェクトはこの例が通用します。
これでもできます。
var values = new List<string> { "Apple","徳川家康","12345" };
string json = System.Text.Json.JsonSerializer.Serialize(values);
string base64 = Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Encode(System.Text.Encoding.UTF8.GetBytes(json));
//WyJBcHBsZSIsIlx1NUZCM1x1NURERFx1NUJCNlx1NUVCNyIsIjEyMzQ1Il0
System.Diagnostics.Debug.WriteLine(base64);
メモ:この例ではオブジェクトをJSON形式で文字列化したものをBase64Urlにしています。
メモ:List<string>に限らず、JsonSerializerでJSON化できるオブジェクトはこの例が通用します。
メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法
バイト型の配列をBase64Urlにエンコードする
byte[] bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 };
string base64 = Convert.ToBase64String(bytes);
base64 = base64.Replace("+", "-").Replace("/", "_").Replace("=", "");
System.Diagnostics.Debug.WriteLine(base64); //EjRWeJq83vA
これでもできます。
byte[] bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 };
string base64 = Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Encode(bytes);
System.Diagnostics.Debug.WriteLine(base64); //EjRWeJq83vA
メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法