1

I have a table in SQL Server with a varbinary(MAX) column which is filled with SQL Compress method of JSON string with utf8 characters for example {"id": 12, title: "فروش"} in the sql I use CAST(DECOMPRESS(data) AS NVARCHAR(MAX)) and result is ok.

In the c# I use this code for decompressing the data column:

public static string Unzip(byte[] bytes)
{
    using (var msi = new MemoryStream(bytes))
    using (var mso = new MemoryStream())
    {
        using (var gs = new GZipStream(msi, CompressionMode.Decompress))
        {
            CopyTo(gs, mso);
        }
        return Encoding.UTF8.GetString(mso.ToArray());
    }
}

But the result is an invalid string:

enter image description here

1
  • 1
    It's DeflateStream. Commented Aug 11, 2020 at 10:55

2 Answers 2

6

I believe your string is in the format "Unicode" but you are trying to use the encoding type UTF8?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. I changed the encoding and every things got right.
You are quite welcome, I just realized how much of a nerd I am, I recognized the unicode format... ugh. I need to re-evaluate my life :D
@mortenBork relax, we all have our own unique nerd super powers. This is yours.
0

Depending on if you have the default Collations enabled in SQL, NVARCHAR will be returned as Unicode while VARCHAR will be returned as UTF-8. You could also use SQL Output Collation to force an encoding:

SELECT CAST('abc' AS varchar(5)) COLLATE Latin1_General_100_CI_AS_SC_UTF8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.