gpt4 book ai didi

c# - byte[] 到文本文件的数据包转储/十六进制 View 格式

转载 作者:行者123 更新时间:2023-12-02 20:35:35 26 4
gpt4 key购买 nike

我想将数据包 (byte[]) 转换为文本文件上的十六进制转储 View ,但它变得困惑。

这是一张图片。

enter image description here

我在主 byte[] 上循环,直到达到 16 的 mod(不包括 0),但是如果最后一行不是 16 的因数,那么最终会导致 ASCII 表示发生移位向左

我确实尝试了几种解决方案,但没有一个真的很不错,所以我认为这是一个盲点,并认为我可以使用一些帮助:)

此外,我还可以使用一些帮助来检查该字符是否具有 ascii 表示形式,或者我应该将其替换为“.”

    using (var sw = File.AppendText(Path))
{
sw.WriteLine("[Packet Logged at {0} with direction {1}] with length {2} and type {3}",
DateTime.Now.ToLongTimeString().Replace("PM", string.Empty).Replace("AM", string.Empty).Replace(" ", string.Empty),
ReadWrite.ReadString(Packet,Packet.Length-8,8) == "removed for privacy" ? "Server->Client" : "Client -> Server",
BitConverter.ToUInt16(Packet, 0), BitConverter.ToUInt16(Packet, 2));
for (int i = 0; i < Packet.Length; i++)
{
if (i%16 == 0 && i != 0)
{

}
else
{
sw.Write(Packet[i]);
}
}
}

最佳答案

摘自http://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array:

using System.Text;

namespace HexDump
{
class Utils
{
public static string HexDump(byte[] bytes, int bytesPerLine = 16)
{
if (bytes == null) return "<null>";
int bytesLength = bytes.Length;

char[] HexChars = "0123456789ABCDEF".ToCharArray();

int firstHexColumn =
8 // 8 characters for the address
+ 3; // 3 spaces

int firstCharColumn = firstHexColumn
+ bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
+ (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
+ 2; // 2 spaces

int lineLength = firstCharColumn
+ bytesPerLine // - characters to show the ascii value
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)

char[] line = (new String(' ', lineLength - 2) + Environment.NewLine).ToCharArray();
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
StringBuilder result = new StringBuilder(expectedLines * lineLength);

for (int i = 0; i < bytesLength; i += bytesPerLine)
{
line[0] = HexChars[(i >> 28) & 0xF];
line[1] = HexChars[(i >> 24) & 0xF];
line[2] = HexChars[(i >> 20) & 0xF];
line[3] = HexChars[(i >> 16) & 0xF];
line[4] = HexChars[(i >> 12) & 0xF];
line[5] = HexChars[(i >> 8) & 0xF];
line[6] = HexChars[(i >> 4) & 0xF];
line[7] = HexChars[(i >> 0) & 0xF];

int hexColumn = firstHexColumn;
int charColumn = firstCharColumn;

for (int j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytesLength)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
byte b = bytes[i + j];
line[hexColumn] = HexChars[(b >> 4) & 0xF];
line[hexColumn + 1] = HexChars[b & 0xF];
line[charColumn] = asciiSymbol( b );
}
hexColumn += 3;
charColumn++;
}
result.Append(line);
}
return result.ToString();
}
static char asciiSymbol( byte val )
{
if( val < 32 ) return '.'; // Non-printable ASCII
if( val < 127 ) return (char)val; // Normal ASCII
// Handle the hole in Latin-1
if( val == 127 ) return '.';
if( val < 0x90 ) return "€.‚ƒ„…†‡ˆ‰Š‹Œ.Ž."[ val & 0xF ];
if( val < 0xA0 ) return ".‘’“”•–—˜™š›œ.žŸ"[ val & 0xF ];
if( val == 0xAD ) return '.'; // Soft hyphen: this symbol is zero-width even in monospace fonts
return (char)val; // Normal Latin-1
}
}
}

关于c# - byte[] 到文本文件的数据包转储/十六进制 View 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26206257/

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