如何将字符串转换为base64字节数组,这将有效吗? [英] How to convert string to base64 byte array, would this be valid?
本文介绍了如何将字符串转换为base64字节数组,这将有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个将字符串转换为base64字节数组的函数.我已经尝试过这种方法:
I'm trying to write a function that converts a string to a base64 byte array. I've tried with this approach:
public byte[] stringToBase64ByteArray(String input)
{
byte[] ret = System.Text.Encoding.Unicode.GetBytes(input);
string s = Convert.ToBase64String(input);
ret = System.Text.Encoding.Unicode.GetBytes(s);
return ret;
}
此函数是否会产生有效的结果(假设字符串采用unicode)? 谢谢!
Would this function produce a valid result (provided that the string is in unicode)? Thanks!
推荐答案
看起来不错,尽管这种方法很奇怪.但是,请使用Encoding.ASCII.GetBytes()将base64字符串转换为byte []. Base64编码仅包含ASCII字符.使用Unicode可以为每个字符额外增加0个字节.
Looks okay, although the approach is strange. But use Encoding.ASCII.GetBytes() to convert the base64 string to byte[]. Base64 encoding only contains ASCII characters. Using Unicode gets you an extra 0 byte for each character.
这篇关于如何将字符串转换为base64字节数组,这将有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文