Skip to content

Instantly share code, notes, and snippets.

@ebubekirbastama
Created December 8, 2023 11:07
Show Gist options
  • Save ebubekirbastama/5029200f0d83a95f3da4d88d6a7c0644 to your computer and use it in GitHub Desktop.
Save ebubekirbastama/5029200f0d83a95f3da4d88d6a7c0644 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
namespace EBSCoder
{
internal class EbsBase64Encoder
{
public string ConvertStringToBase64(string input)
{
string binaryRepresentation = ConvertStringToBinary(input);
string base64Result = ConvertBinaryToBase64(binaryRepresentation);
return base64Result;
}
private string ConvertStringToBinary(string input)
{
StringBuilder binaryResult = new StringBuilder();
foreach (char c in input)
{
binaryResult.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
return binaryResult.ToString();
}
private string ConvertBinaryToBase64(string binaryStr)
{
const string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Eksik bitleri doldur
while (binaryStr.Length % 6 != 0)
{
binaryStr += '0';
}
StringBuilder result = new StringBuilder();
// 6 bitlik bloklara ayır ve Base64'e çevir
for (int i = 0; i < binaryStr.Length; i += 6)
{
string block = binaryStr.Substring(i, 6);
int decimalValue = Convert.ToInt32(block, 2);
result.Append(base64Chars[decimalValue]);
}
return result.ToString();
}
}
}
//Kullanımı
EbsBase64Encoder converter = new EbsBase64Encoder();
string inputText = "123";
string base64Result = converter.ConvertStringToBase64(inputText);
Console.WriteLine($"Original: {inputText}\nBase64: {base64Result}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment