Kezdőlap

Cégismertető
Partnereink
Kapcsolat

Eszközök
Quoted-printable kódoló
Base64 kódoló
Html kódoló
Url kódoló
Guid generátor
Machine key generátorÚj









Base64 encoding easily (Magyar verzió)

The Microsoft .net framework supports the base64 encoding from the bases. The encoding can be done with the System.Convert class. To convert a simple UTF-8 coded text into a Base64 string in C# you can use this:

string result = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(utf8Text));

The ToBase64String method gets a byte array as a parameter and gives the encoded string back. The result can be stored in a simple string, because the base64 string is a readable ASCII string.

Decoding

To decode a UTF-8 text is a slightly more difficult than the encoding.

byte[] toDecodeByte = Convert.FromBase64String(textToTransform);

System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.
Decoder utf8Decode = encoder.GetDecoder();

int
charCount = utf8Decode.GetCharCount(toDecodeByte, 0, toDecodeByte.Length);

char
[] decodedChar = new char[charCount];
utf8Decode.GetChars(toDecodeByte, 0, toDecodeByte.Length, decodedChar, 0);
string result =
new String(decodedChar);

The decoding - like before - can be done with calling a method from the Convert class. The only problem with this, that is gives back a byte array as a result. It should be converted to a UTF-8 string with the System.Text namespace's Decoder class.

Links on the site

 



Copyright © 2024 NOWAN Informatikai Szolgáltató és Tanácsadó Bt.