文字內容轉換為十六進位制格式字串對照表
阿新 • • 發佈:2019-02-13
直接上程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Tools.PSMPDebug
{
///
/// 介面區域型別
///
public enum HitTestType
{
///
/// 十六進位制區
///
Hex,
///
/// 字元區
///
Char
}
/// <summary> /// HexHelper /// </summary> public class HexHelper { /// <summary> /// 當前處理的資料流 /// </summary> private MemoryStream memoryStream; /// <summary> /// 儲存繪畫的行資料 /// </summary> private Dictionary<int, string> dicDraw; /// <summary> /// 總行數 /// </summary> private int lineCount = 0; /// <summary> /// 獲取文字內容的十六進位制格式字串 /// </summary> /// <param name="text">文字內容</param> /// <returns>十六進位制格式字串</returns> public string GetHexContextByText(string text) { if (!string.IsNullOrEmpty(text)) { try { byte[] arry = Encoding.UTF8.GetBytes(text); memoryStream = new MemoryStream(arry); dicDraw = new Dictionary<int, string>(); lineCount = (int)(memoryStream.Length / 16 + 1); for (int i = 0; i < lineCount; i++) { int currentLine = i; if (currentLine >= lineCount) break; memoryStream.Position = currentLine * 16; byte[] vBuffer = new byte[16]; int vLength = memoryStream.Read(vBuffer, 0, vBuffer.Length); string addressStr = DrawAddress(currentLine); string hexStr = DrawHex(vBuffer, vLength); string charStr = DrawChar(vBuffer, vLength); if (!dicDraw.ContainsKey(currentLine)) { dicDraw.Add(currentLine, addressStr + " " + hexStr + " " + charStr); } } if (dicDraw != null && dicDraw.Count > 0) { string str = string.Empty; foreach (KeyValuePair<int, string> item in dicDraw) { str += item.Value + "\r\n"; } return str; } } catch (Exception) { throw; } finally { if (memoryStream != null) { memoryStream.Close(); } } } return string.Empty; } /// <summary> /// 繪製地址區塊。 /// </summary> /// <param name="line">行標</param> private string DrawAddress(int line) { return (line * 16).ToString("X8"); } /// <summary> /// 繪製十六進位制區塊。 /// </summary> /// <param name="buf">緩衝區</param> /// <param name="len">緩衝長度</param> private string DrawHex(byte[] buf, int len) { return ViewText(HitTestType.Hex, buf, len, 0, len); } /// <summary> /// 繪製字元區塊。 /// </summary> /// <param name="buf">緩衝區</param> /// <param name="len">緩衝長度</param> private string DrawChar(byte[] buf, int len) { return ViewText(HitTestType.Char, buf, len, 0, len); } /// <summary> /// 獲取在編輯框顯示字元。 /// </summary> /// <param name="type">區域型別</param> /// <param name="buf">資料</param> /// <param name="len">緩衝長度</param> /// <param name="start">起始位置</param> /// <param name="end">結束位置</param> /// <returns>返回顯示字元</returns> private string ViewText(HitTestType type, byte[] buf, int len, int start, int end) { if (len <= 0) return string.Empty; const string vCharHexs = "0123456789ABCDEF"; StringBuilder vBuffer = new StringBuilder(128); start = Math.Max(0, start); end = Math.Min(len - 1, end); for (int i = start; i <= end; i++) { switch (type) { case HitTestType.Hex: if (i == 8) vBuffer.Append(" "); vBuffer.Append(vCharHexs[buf[i] >> 4]); vBuffer.Append(vCharHexs[buf[i] & 0x0F]); vBuffer.Append(" "); break; case HitTestType.Char: if (buf[i] >= 32 && buf[i] <= 126) vBuffer.Append((char)buf[i]); else vBuffer.Append('.'); break; } } if (type == HitTestType.Hex) return vBuffer.ToString().Trim(); else return vBuffer.ToString(); } }
}
效果展示