简体   繁体   English

如何将字符串转换为base64字节数组,这是否有效?

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

I'm trying to write a function that converts a string to a base64 byte array. 我正在尝试编写一个将字符串转换为base64字节数组的函数。 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)? 此函数是否会产生有效结果(假设字符串是unicode)? Thanks! 谢谢!

You can use: 您可以使用:

From byte[] to string: 从byte []到string:

byte[] array = somebytearray;

string result = Convert.ToBase64String(array);

From string to byte[]: 从字符串到字节[]:

array = Convert.FromBase64String(result);

Looks okay, although the approach is strange. 看起来没问题,虽然方法很奇怪。 But use Encoding.ASCII.GetBytes() to convert the base64 string to byte[]. 但是使用Encoding.ASCII.GetBytes()将base64字符串转换为byte []。 Base64 encoding only contains ASCII characters. Base64编码仅包含ASCII字符。 Using Unicode gets you an extra 0 byte for each character. 使用Unicode会为每个字符增加一个0字节。

Representing a string as a blob represented as a string is odd... any reason you can't just use the string directly? 将字符串表示为表示为字符串的blob是奇怪的...任何原因你不能直接使用字符串?

The string is always unicode; 字符串总是 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). 由于base-64总是<128,所以在最后一部分使用unicode似乎有点过分(除非这是线格式要求的)。 Personally, I'd use UTF8 or ASCII for the last GetBytes so that each base-64 character only takes one byte. 就个人而言,我会使用UTF8或ASCII作为最后的GetBytes这样每个base-64字符只占用一个字节。

All strings in .NET are unicode. .NET中的所有字符串都是unicode。 This code will produce valid result but the consumer of the BASE64 string should also be unicode enabled. 此代码将生成有效结果,但BASE64字符串的使用者也应启用unicode。

Yes, it would output a base64-encoded string of the UTF-16 little-endian representation of your source string. 是的,它将输出源字符串的UTF-16 little-endian表示的base64编码字符串。 Keep in mind that, AFAIK, it's not really common to use UTF-16 in base64, ASCII or UTF-8 is normally used. 请记住,AFAIK,通常使用base64,ASCII或UTF-8中使用UTF-16并不常见。 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. 我不明白你为什么要在字节数组中重新转换base64字符串:base64用于避免在传输时编码不兼容,所以你应该保持字符串并以你用来传输数据的协议所需的格式输出它。 And, as Marc said, it's definitely overkill to use UTF-16 for that purpose, since base64 includes only 64 characters, all under 128. 而且,正如Marc所说,使用UTF-16绝对是不合适的,因为base64只包含64个字符,全部在128以下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将逐个字节数组将chunck转换为base64字符串? - How to convert chunck by chunck byte array into base64 string? 将Java字节数组转换为C#base64字符串 - Convert java byte array to C# base64 string 可以将byte []数组转换为Base64字符串吗? - Can a byte[] array ever not convert to a Base64 string? 在将pdf作为base64字符串转换为字节数组时,输入不是有效的Base-64字符串 - The input is not a valid Base-64 string on converting a pdf as base64 string to byte array 如何在JavaScript中使用字节数组将字符串转换为base64编码? - How to convert a string to base64 encoding using byte array in JavaScript? 将base64字符串转换为字节数组,如C#方法Convert.FromBase64String - convert base64 string to byte array like C# method Convert.FromBase64String 将原始 RGB 字节数组转换为 JPG 编码字符串 base64 - Convert a raw RGB byte array to a JPG encoded string base64 使用缓冲区将byte []转换为Base64字符串(以避免OOM) - Convert byte[] to Base64 string with buffer (to avoid OOM) Web Api 为每个查询将字节数组转换为 base64 - Web Api convert byte array to base64 for every query 使用 Base64 编码将字节数组转换为 SecureString 的安全方法 - Secure way to convert a byte array to SecureString with Base64 encoding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM