110,569
社区成员
发帖
与我相关
我的任务
分享
public Bitmap GetBitmap()
{
byte[] bytes = Fix0d0d0a(OutStream.ToArray());
using (MemoryStream pngStream = new MemoryStream(bytes))
{
return new Bitmap(pngStream);
}
}
private byte[] Fix0d0d0a(byte[] bytes)
{
long length = bytes.Length;
byte[] bytesfix = new byte[length];
int idx = 0;
int count = 0;
int idxFirst0D = 0;
int idxFirst0A = 0;
bool is0D = false;
for (int i = 0; i < length; i++)
{
byte b = bytes[i];
if (b == 0x0d && idxFirst0D == 0)
{
idxFirst0D = i;
is0D = true;
}
if (b == 0x0a && idxFirst0A == 0)
{
idxFirst0A = i;
}
if (i > 2 && b == 0x0a && is0D)
{
count++;
idx = idx - (idxFirst0A - idxFirst0D - 1);
bytesfix[idx] = b;
idx++;
}
else
{
bytesfix[idx] = b;
idx++;
}
if (b == 0x0d)
is0D = true;
else
is0D = false;
}
byte[] bytesfinal = new byte[length - count * (idxFirst0A - idxFirst0D - 1)];
Buffer.BlockCopy(bytesfix, 0, bytesfinal, 0, bytesfinal.Length);
return bytesfinal;
}
private IEnumerable<byte> fix(byte[] source)
{
for (var i = 0; i < source.Length; i++)
{
if (source[i] == 0x0d && source[i + 1] == 0x0d && source[i + 2] == 0x0a)
{
i += 1;
}
else
{
yield return source[i];
}
}
}
此外,还是有疑惑,
1、为啥安卓将0a编码成0d0d0a,有什么特别意义么。
2、代码中,idx = idx - (idxFirst0A - idxFirst0D - 1),为啥不能直接idx-2,仅仅是原作者随便写的,还是因为这中间的距离会有什么变化? private static byte[] bba = new byte[] {0x0b, 0x0b, 0x0c};
static void Main(string[] args)
{
byte[] source = new byte[] { 0x0b, 0x0b, 0x0c };
var res = fix(source).ToList();
}
static IEnumerable<byte> fix(byte[] source)
{
for (var i = 0; i < source.Length; i++)
{
if (!source.Skip(i).Take(3).SequenceEqual(bba))
yield return source[i];
}
}
}