gpt4 book ai didi

c# - 如何将 byte[] 转换为该文本格式?

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:33 25 4
gpt4 key购买 nike

我可以说我不知道​​我在寻求帮助,因为我不知道格式,但我有一张图片。

我有一个 byte[] 数组,如何将其转换为下面(右)的格式?

alt text http://img512.imageshack.us/img512/3548/48667724.jpg

它不是普通的 ascii。

最佳答案

听起来你想获取一个字节数组,并将其转换为文本(用“.”s 替换特定范围之外的字符)

static public string ConvertFromBytes(byte[] input)
{
StringBuilder output = new StringBuilder(input.Length);

foreach (byte b in input)
{
// Printable chars are from 0x20 (space) to 0x7E (~)
if (b >= 0x20 && b <= 0x7E)
{
output.Append((char)b);
}
else
{
// This isn't a text char, so use a placehold char instead
output.Append(".");
}
}

return output.ToString();
}

或作为 LINQy 扩展方法(在静态扩展类中):

static public string ToPrintableString(this byte[] bytes)
{
return Encoding.ASCII.GetString
(
bytes.Select(x => x < 0x20 || x > 0x7E ? (byte)'.' : x)
.ToArray()
);
}

(你可以这样称呼 string printable = byteArray.ToPrintableString();)

关于c# - 如何将 byte[] 转换为该文本格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/812716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com