简体   繁体   中英

Cannot append value to string C#

I've got two methods in my Windows Forms application for parsing some gzip encoded strings.

Here are my two methods, which both return the same value.

public static async Task<string> DecodeGzipAsync(string str)
{
    var values = new Dictionary<string, string>
    {
        { "data", str }
    };

    var content = new FormUrlEncodedContent(values);

    var client = new HttpClient();

    var response = await client.PostAsync("http://www.txtwizard.net/compression/decompress/gz", content);

    var json = await response.Content.ReadAsStringAsync();

    var result = JsonConvert.DeserializeObject<GzipData>(json);

    return result.DecompressedData;
}

public static string DecodeGzip(string str)
{
    byte[] gzBuffer = Convert.FromBase64String(str);

    using (MemoryStream ms = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzBuffer, 0);
        ms.Write(gzBuffer, 0, gzBuffer.Length);

        byte[] buffer = new byte[msgLength];

        ms.Position = 0;

        using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
        {
            zip.Read(buffer, 0, buffer.Length);
        }

        return Encoding.UTF8.GetString(buffer);

    }

And then I call these two methods from my main form.

var string1 = await Utilities.DecodeGzipAsync(xml.InnerText);
var string1Concat = string.Concat("<Connotes>", string1, "</Connotes>");

var string2 = Utilities.DecodeGzip(xml.InnerText);
var string2Concat = string.Concat("<Connotes>", string2, "</Connotes>");

Where xml.InnerText is

H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ih6fVMtl1ebp6bIt2uvPPto/ffnR0ePff7qu63w5vT763R7fdX88zqbTZXW0e3Bv5/7ju/LH48m6batl07RZmx/tuOfx3eCbx9Osrou8LmZH+w92H991f5pvltkiPzr58uTbr45fnL5On3/5+WvbjL/jhj+olvnR652H8hX/9Xg6z+qLvK2OXuXTvLjMa/rSfPR4Wi1nQODe7t2d3bt7O7sH9K1+Zr4s84uMxvfg4d7DPfutfog2NM6T4zc7O/sP7+3y90sBDNIt14tJX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vCSv1Y1e0jfvZ/y/xc/4sITWk1dffvX5t9+oFxD1Tr795asXTyj18+Xx075n4r2/yTvZ2/sa3gm5Qy9OB72T+wf36NnhFbaH9wayWBa99An5VhtdlPvp3qc7Bz9yUX7ILopo29slrx5+sIdyPoOdHkpePdwf7+7v3Ht48On93Z1Pdwc8lP8Hq6xfMfkjAAA=

The results I get are really frustrating (and weird!)

string1 and string2 both return the same string, however string1Concat and string2Concat return different results.

string1Concat = "<Connotes>" + string1 + "</Connotes>"

string2Concat = "<Connotes>" + string2  // Notice how it is missing the concatenation on the end     

What might be causing this behaviour? I have tried changing the encoding to ASCII and UTF8 but both give the same results.

Any help is much appreciated!

This was a little tricky, but I found the problem/solution:
You convert the array of the size of the compressed data to a string at Encoding.UTF8.GetString(buffer) . This will create a string which a length of 559903 instead of the correct 9209. If you create a new byte-array with the actually read bytes it is working:

public static string DecodeGzip(string str)
{
    byte[] gzBuffer = Convert.FromBase64String(str);

    using (MemoryStream ms = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzBuffer, 0);
        ms.Write(gzBuffer, 0, gzBuffer.Length);

        byte[] buffer = new byte[msgLength];

        ms.Position = 0;
        int length;
        using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
        {
            length = zip.Read(buffer, 0, buffer.Length);
        }

        var data = new byte[length];
        Array.Copy(buffer, data, length);
        return Encoding.UTF8.GetString(data);   
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM