我有一个textbox,在里面显示一个16进制数“AE”,如果保存这个字符串为一个文本文件后,要占两个字节,用hex查看实际保存的是“41” “45”,现在我想让这个字符串占一个字节,也就是用hex查看应该也是“AE” ,求教大家这个用什么方法??

解决方案 »

  1.   

    用(byte)(int.Parse(txt, NumberStyles.AllowHexSpecifier))。如果字符串包含多个字节的话,就依次取出两个得到对应的字节。
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "AE";
                byte b = (byte)(Convert.ToInt32(s, 16) % 256);
                using (FileStream fs = new FileStream("c:\\test.dat", FileMode.OpenOrCreate))
                {
                    fs.WriteByte(b);
                }
            }
        }
    }
      

  3.   

    byte bt = (byte)(Convert.ToInt32("AE", 16));