无法将值附加到字符串 C#
Cannot append value to string C#
我的 Windows Forms 应用程序中有两种方法可以解析一些 gzip 编码的字符串。
这是我的两种方法,它们return 的值相同。
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);
}
然后我从主窗体中调用这两个方法。
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>");
其中 xml.InnerText 是
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=
我得到的结果真的很令人沮丧(而且很奇怪!)
string1 和 string2 return 相同的字符串,但是 string1Concat 和 string2Concat return 不同的结果。
string1Concat = "<Connotes>" + string1 + "</Connotes>"
string2Concat = "<Connotes>" + string2 // Notice how it is missing the concatenation on the end
可能是什么导致了这种行为?我曾尝试将编码更改为 ASCII 和 UTF8,但两者的结果相同。
非常感谢任何帮助!
这有点棘手,但我找到了 problem/solution:
您将压缩数据大小的数组转换为 Encoding.UTF8.GetString(buffer)
处的字符串。这将创建一个长度为 559903 而不是正确的 9209 的字符串。如果您使用实际读取的字节创建一个新的字节数组,它正在工作:
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);
}
}
我的 Windows Forms 应用程序中有两种方法可以解析一些 gzip 编码的字符串。
这是我的两种方法,它们return 的值相同。
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);
}
然后我从主窗体中调用这两个方法。
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>");
其中 xml.InnerText 是
H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ih6fVMtl1ebp6bIt2uvPPto/ffnR0ePff7qu63w5vT763R7fdX88zqbTZXW0e3Bv5/7ju/LH48m6batl07RZmx/tuOfx3eCbx9Osrou8LmZH+w92H991f5pvltkiPzr58uTbr45fnL5On3/5+WvbjL/jhj+olvnR652H8hX/9Xg6z+qLvK2OXuXTvLjMa/rSfPR4Wi1nQODe7t2d3bt7O7sH9K1+Zr4s84uMxvfg4d7DPfutfog2NM6T4zc7O/sP7+3y90sBDNIt14tJXve+t1+Ydqu6WLb57Oj3sQ3MJ2ixqs/fAQZ/yX/g06a4WC7yZQt0FBwGsLOr3US+f2xn6/irp9TIzt0sK8prIvfu/gFmz/xpPv/9r7LlEc1++DV/+pipUS0vvM7tR49z5hxM6+nLx3ftX4/P67y4mLdlscxneUvgGuKfRXtEL+MH4Tkhfhnv7N6/BzTpj8ezvJkSEV69+fIF9YA/Hs8v6A2abPx8XObLI3SOn4/r/Pxo997BvU/vp2n67Nnju/jg8dWsPdr79PFd/Hx8RS/RfOLH47sxfM7raqEMtYMW+tfjizpbztqqX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eKQvjkld+TkNDDSx189+erVZgu9f/8mCx1zT744fvV7DfomO3sPyNPcYYv7KXnA9z/d6fsmMtL02fGrL8i7Ovn22YvTV79P+ur05fHZq9dxn2X/Ifksn97/kc/yw/ZZWAHfzme5/8E+y2S28/sM+yz3Ph0f7O98+pD+f//+7v1v1GfZZ0/959JnuSdf/fB9FrEOwz6L+f7/zT4LuHeDz+J9/bPvs+wdfLp/g8/yYM/zWR4chD7L3t5Bx2WBmnaZkxt8llvhuLMHmBtxRLrA+VWfhjju7+123ap9D8dP/7/vVj149uzk3rMH97bvPznY2d7ffXa6/fDB0/3tT/d2H5zuHdzfuX/6bNitGsgYPXx4+umz/Z3tB08/fbi9f/rpyfaTe7un2zv37z97und8vPPw9P4HuGr3buWqea02umrkTbE3/Q25ajdj/+RW2LtWN2B/H/nSbwr7/5c5mtZFfHV28u3jV09fD3uN93Z20lfHZ8+/e/z7pG86LuLLL1+9SV+evTqTjwezWPtfJ4v1+uwLKJSon3hvvPtg54DypOzlUAri4P6nD/p+ohld+sWXb74c8Aw/vbeX7u0/2P+RZ/hD9gzv7cIF63uGexHPELT7QNewmCGrM+AakiSNyW7u7j64d59+7H+jruHug/uf/nz1Dff5+2Hf0Hz//2rfcHezb+i+/tn3DcnSw8Jv8rvu+b4hvLQgn/XwQdpNaGESrOf18P/7ntfDT09OTsg6bJMwk+f1dH9v+8ne8UP688HJ/U+PyWo8+JCE1v6tvCSv1Y1e0jfvZ/y/xc/4sITWk1dffvX5t9+oFxD1Tr795asXTyj18+Xx075n4r2/yTvZ2/sa3gm5Qy9OB72T+wf36NnhFbaH9wayWBa99An5VhtdlPvp3qc7Bz9yUX7ILopo29slrx5+sIdyPoOdHkpePdwf7+7v3Ht48On93Z1Pdwc8lP8Hq6xfMfkjAAA=
我得到的结果真的很令人沮丧(而且很奇怪!)
string1 和 string2 return 相同的字符串,但是 string1Concat 和 string2Concat return 不同的结果。
string1Concat = "<Connotes>" + string1 + "</Connotes>"
string2Concat = "<Connotes>" + string2 // Notice how it is missing the concatenation on the end
可能是什么导致了这种行为?我曾尝试将编码更改为 ASCII 和 UTF8,但两者的结果相同。
非常感谢任何帮助!
这有点棘手,但我找到了 problem/solution:
您将压缩数据大小的数组转换为 Encoding.UTF8.GetString(buffer)
处的字符串。这将创建一个长度为 559903 而不是正确的 9209 的字符串。如果您使用实际读取的字节创建一个新的字节数组,它正在工作:
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);
}
}