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

后端 未结 5 2046
温柔的废话
温柔的废话 2021-02-03 20:20

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         


        
5条回答
  •  爱一瞬间的悲伤
    2021-02-03 21:19

    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.

提交回复
热议问题