// Codec.cs - Codec interface
// (ignore namespace)
interface Codec {
string Encode(string s);
string Decode(string s);
}
Next, we provide a Base64 codec implementation to support Codec interface.
// Base64Codec.cs - Base64 Codec implementation
// (ignore namespace)
using System;
using System.Text;
public class Base64Codec : Codec {
public string Encode(string s) {
return Convert.ToBase64String(Encoding.UTF8.GetBytes(s));
}
public string Decode(string s) {
byte[] decodedBytes = Convert.FromBase64String(s);
Decoder decoder = new UTF8Encoding().GetDecoder();
int size = decoder.GetCharCount(decodedBytes,
0,
decodedBytes.Length);
char[] decodedChars = new char[size];
decoder.GetChars(decodedBytes,
0,
decodedBytes.Length,
decodedChars,
0);
return new String(decodedChars);
}
}
Done.
沒有留言:
張貼留言