C#十六进制转

BenBenBears 2013-08-28 08:50:31

string text = ScreenBox.Text;
ScreenBox.Clear();
byte[] textToByte = Encoding.Default.GetBytes(text);
for (int i = 0; i < textToByte.Length; i++)
{
byte temp = textToByte[i];
string tempHex = temp.ToString("X2") + " ";
ScreenBox.Text += tempHex;
}

上面的代码是将ScreenBox控件中的文本转换为十六进制并输出在ScreenBox上;然后我再将新的文本拷贝下来。

string Hextext = ScreenBox.Text;

最后我想将Hextext字符串转换为原先的text字符串并输出,怎么写代码?谢谢。
...全文
186 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
智商余额不足 2013-08-28
  • 打赏
  • 举报
回复

string Hextext = ScreenBox.Text;
var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
string value=Encoding.Default.GetString(hexItems);
bdmh 2013-08-28
  • 打赏
  • 举报
回复
取每两个16进制字符,然后convert成int,然后写入byte,在encoding编码为字符
爱LOVE大葱 2013-08-28
  • 打赏
  • 举报
回复
///<summary>
        /// 从16进制转换成汉字
        /// </summary>
        /// <param name="hex"></param>
        /// <param name="charset">编码,如"utf-8","gb2312"</param>
        /// <returns></returns>
        public static string UnHex(string hex, string charset)
        {
            if (hex == null)
                throw new ArgumentNullException("hex");
            hex = hex.Replace(",", "");
            hex = hex.Replace("\n", "");
            hex = hex.Replace("\\", "");
            hex = hex.Replace(" ", "");
            if (hex.Length % 2 != 0)
            {
                hex += "20";//空格
            }
            // 需要将 hex 转换成 byte 数组。 
            byte[] bytes = new byte[hex.Length / 2];

            for (int i = 0; i < bytes.Length; i++)
            {
                try
                {
                    // 每两个字符是一个 byte。 
                    bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                    System.Globalization.NumberStyles.HexNumber);
                }
                catch
                {
                    // Rethrow an exception with custom message. 
                    throw new ArgumentException("hex is not a valid hex number!", "hex");
                }
            }
            System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
            return chs.GetString(bytes);
        }
BenBenBears 2013-08-28
  • 打赏
  • 举报
回复
引用 7 楼 FoxDave 的回复:
以0x开头的是十六进制
数据用byte[]表示,比如byte[2],byte[0]=0,byte[1]=1,我们怎么判断才能知道byte[2]表示是01的十六进制还是01表示的字符。
BenBenBears 2013-08-28
  • 打赏
  • 举报
回复
引用 6 楼 hwenycocodq520 的回复:
[quote=引用 4 楼 BenBenBears 的回复:] [quote=引用 3 楼 hwenycocodq520 的回复:]

string Hextext = ScreenBox.Text;
var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
string value=Encoding.Default.GetString(hexItems);
能简单的解释一下第二行吗?谢谢。

var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
[/quote] Hextext.Split(' '):根据空格分隔字符串得到子字符串数组; Where(s=>s!=""):筛选数组中不为空串的元素; Select(s => (byte)Convert.ToInt32(s,16)):将序列中的元素进行类型转换并投影到新表中; ToArray():转换成byte[]数组[/quote] 抱歉,ToArray()确实是返回byte[]型的数据,我调试时添加监视了。不知道是我自己的问题还是编译器的问题,明明是正确的参数类型,还是下划线说无效的参数类型。你的代码比楼上的朋友简洁,我采用你的方案,谢谢。
BenBenBears 2013-08-28
  • 打赏
  • 举报
回复
引用 6 楼 hwenycocodq520 的回复:
[quote=引用 4 楼 BenBenBears 的回复:] [quote=引用 3 楼 hwenycocodq520 的回复:]

string Hextext = ScreenBox.Text;
var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
string value=Encoding.Default.GetString(hexItems);
能简单的解释一下第二行吗?谢谢。

var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
[/quote] Hextext.Split(' '):根据空格分隔字符串得到子字符串数组; Where(s=>s!=""):筛选数组中不为空串的元素; Select(s => (byte)Convert.ToInt32(s,16)):将序列中的元素进行类型转换并投影到新表中; ToArray():转换成byte[]数组[/quote] ToArray()返回的是int型的数组,因为前面新表返回的是int型,这样下面的GetString就不能用了。
Justin-Liu 2013-08-28
  • 打赏
  • 举报
回复
以0x开头的是十六进制
智商余额不足 2013-08-28
  • 打赏
  • 举报
回复
引用 4 楼 BenBenBears 的回复:
[quote=引用 3 楼 hwenycocodq520 的回复:]

string Hextext = ScreenBox.Text;
var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
string value=Encoding.Default.GetString(hexItems);
能简单的解释一下第二行吗?谢谢。

var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
[/quote] Hextext.Split(' '):根据空格分隔字符串得到子字符串数组; Where(s=>s!=""):筛选数组中不为空串的元素; Select(s => (byte)Convert.ToInt32(s,16)):将序列中的元素进行类型转换并投影到新表中; ToArray():转换成byte[]数组
BenBenBears 2013-08-28
  • 打赏
  • 举报
回复
引用 2 楼 bdmh 的回复:
取每两个16进制字符,然后convert成int,然后写入byte,在encoding编码为字符
版主大人,谢谢你给的思路,上面的问题解决了。另外请问怎么去判断由串口读入的一组数据是十六进制还是十进制的,试过正则表达式,不管用。
BenBenBears 2013-08-28
  • 打赏
  • 举报
回复
引用 3 楼 hwenycocodq520 的回复:

string Hextext = ScreenBox.Text;
var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();
string value=Encoding.Default.GetString(hexItems);
能简单的解释一下第二行吗?谢谢。

var hexItems=Hextext.Split(' ').Where(s=>s!="").Select(s => (byte)Convert.ToInt32(s,16)).ToArray();

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧