文本内容转换为十六进制格式字符串对照表

直接上代码

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();
    }
}

}

效果展示
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值