Can the Encoding API decode a Stream/noncontinuous bytes?

后端 未结 3 1718
[愿得一人]
[愿得一人] 2021-01-28 06:49

Usually we can get a string from a byte[] using something like

var result = Encoding.UTF8.GetString(bytes);

However,

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 07:17

    Working code based on Henk's answer using StreamReader:

        using (var memoryStream = new MemoryStream())
        {
            using (var reader = new StreamReader(memoryStream))
            {
                foreach (var byteSegment in bytes)
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    await memoryStream.WriteAsync(byteSegment, 0, byteSegment.Length);
                    memoryStream.Seek(0, SeekOrigin.Begin);
    
                    Debug.WriteLine(await reader.ReadToEndAsync());
                }
            }
        }
    

提交回复
热议问题