Search code examples
c#sql-serverutf-8encode

UTF8 encoding to base64string and storing to database


I'm currently trying to encode data before storing it to my database.

try
{
    byte[] byteEncString = new byte[_strToConvert.Length];
    byteEncString = System.Text.Encoding.UTF8.GetBytes(_strToConvert);
    string strEncoded = Convert.ToBase64String(byteEncString);
    return strEncoded;
}

Does anybody know how long a 15 character string will be after it is encoded via utf8 and base64string? Also, is there a max? My field on sql server is only 50 and i want to limit it in that range. Thoughts?


Solution

  • Well, for one thing there's no point in creating a byte array and then ignoring it - so your code would be simpler as:

    byte[] byteEncString = Encoding.UTF8.GetBytes(_strToConvert);
    return Convert.ToBase64String(byteEncString);
    

    A .NET char can end up as 3 bytes when UTF-8 encoded1, so that gets to a 45 byte maximum. However, base64 converts 3 bytes to 4 characters, so that gives a 60 character maximum base64 encoded string as the result.


    1 This is because any characters not in the basic multilingual plane are represented as a surrogate pair. That pair would end up as 4 bytes, but having taken 2 input characters, so the average "bytes per char" in that case is only 2.