天天看點

Base64的解碼和編碼

把utf8的string轉化為base64的string:

Base64的解碼和編碼
Base64的解碼和編碼

public string base64encode(string data)

{

try

byte[] encdata_byte = new byte[data.length];

encdata_byte = system.text.encoding.utf8.getbytes(data);

string encodeddata = convert.tobase64string(encdata_byte);

return encodeddata;

}

catch(exception e)

throw new exception("error in base64encode" + e.message);

Base64的解碼和編碼

把base64的string轉化為utf8的string:

Base64的解碼和編碼
Base64的解碼和編碼

public string base64decode(string data)

system.text.utf8encoding encoder = new system.text.utf8encoding();

system.text.decoder utf8decode = encoder.getdecoder();

byte[] todecode_byte = convert.frombase64string(data);

int charcount = utf8decode.getcharcount(todecode_byte, 0, todecode_byte.length);

char[] decoded_char = new char[charcount];

utf8decode.getchars(todecode_byte, 0, todecode_byte.length, decoded_char, 0);

string result = new string(decoded_char);

return result;

throw new exception("error in base64decode" + e.message);

Base64的解碼和編碼

最主要的是使用system.convert