How to convert string to base64 byte array, would this be valid?

自作多情 提交于 2020-01-11 21:53:26

问题


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;
}

Would this function produce a valid result (provided that the string is in unicode)? Thanks!


回答1:


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.




回答2:


You can use:

From byte[] to string:

byte[] array = somebytearray;

string result = Convert.ToBase64String(array);

From string to byte[]:

array = Convert.FromBase64String(result);




回答3:


Representing a string as a blob represented as a string is odd... any reason you can't just use the string directly?

The string is always unicode; it is the encoded bytes that change. Since base-64 is always <128, using unicode in the last part seems overkill (unless that is what the wire-format demands). Personally, I'd use UTF8 or ASCII for the last GetBytes so that each base-64 character only takes one byte.




回答4:


All strings in .NET are unicode. This code will produce valid result but the consumer of the BASE64 string should also be unicode enabled.




回答5:


Yes, it would output a base64-encoded string of the UTF-16 little-endian representation of your source string. Keep in mind that, AFAIK, it's not really common to use UTF-16 in base64, ASCII or UTF-8 is normally used. However, the important thing here is that the sender and the receiver agree on which encoding must be used.

I don't understand why you reconvert the base64 string in array of bytes: base64 is used to avoid encoding incompatibilities when transmitting, so you should keep is as a string and output it in the format required by the protocol you use to transmit the data. And, as Marc said, it's definitely overkill to use UTF-16 for that purpose, since base64 includes only 64 characters, all under 128.



来源:https://stackoverflow.com/questions/2295408/how-to-convert-string-to-base64-byte-array-would-this-be-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!