Base64 in C# and .NET

게시

이 가이드에서

This developer guide on Base64Encoding explains csharp in plain language for developers and everyday users.

Everything important stays private: try the matching converter or tool in your browser — files and tokens are not uploaded.

Read the full English article below for code samples and edge cases, or switch language anytime from the menu. We keep expanding translations from real user feedback.

다음으로 이 비공개 도구를 써 보세요

.NET exposes Base64 through the static Convert class, the high-performance System.Buffers.Text.Base64 API, and since .NET 9 a dedicated Base64Url class.

Strings

using System.Text;

string b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("Grüße 👋"));
string text = Encoding.UTF8.GetString(Convert.FromBase64String(b64));

Safe decoding without exceptions

Convert.FromBase64String throws FormatException for invalid input. Use TryFromBase64String with a buffer instead:

Span<byte> buffer = new byte[input.Length * 3 / 4];
if (Convert.TryFromBase64String(input, buffer, out int written))
{
    var data = buffer[..written];
}

FromBase64String ignores whitespace (spaces, tabs, CR, LF) but rejects any other character, including the URL-safe - and _.

Base64URL

// .NET 9+
using System.Buffers.Text;
string token = Base64Url.EncodeToString(bytes);
byte[] back = Base64Url.DecodeFromChars(token);

// Older versions
static string ToBase64Url(byte[] b) =>
    Convert.ToBase64String(b).TrimEnd('=').Replace('+', '-').Replace('/', '_');

ASP.NET Core also offers WebEncoders.Base64UrlEncode in Microsoft.AspNetCore.WebUtilities.

Line breaks

Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks); // 76 chars per line

Files and streams

string b64 = Convert.ToBase64String(await File.ReadAllBytesAsync("photo.jpg"));
await File.WriteAllBytesAsync("copy.jpg", Convert.FromBase64String(b64));

// Streaming:
using var input = File.OpenRead("big.bin");
using var output = File.Create("big.b64");
using var transform = new CryptoStream(output, new ToBase64Transform(), CryptoStreamMode.Write);
await input.CopyToAsync(transform);

Image to data URI (ASP.NET)

var src = $"data:image/png;base64,{Convert.ToBase64String(pngBytes)}";

Verify results with the Base64 decoder.

자주 묻는 질문

Does this page upload my data?

No. Articles are static. Linked tools run locally in your browser unless a feature explicitly needs a network relay.

Can I request a translation or a new tool?

Yes — use the private feedback form. We prioritize what users ask for across languages.

What is the fastest way to try csharp?

Open the homepage Base64 converter or the related tools linked on this page and paste a sample.

관련 도구 및 가이드

댓글 (0)

“Base64 in C# and .NET”에 대한 질문, 아이디어, 버그가 있으신가요? 댓글을 남기거나 도구를 평가하거나 다른 사람에게 답글을 달아 주세요.

아직 댓글이 없습니다. 첫 번째로 의견을 남겨 주세요!

댓글 남기기

이 도구 평가:
Esc
↑ ↓ 이동↵ 열기Esc 닫기