C#二进制与字符串互转换,十六进制转换为字符串、float、int

/// <summary>     /// 将 字符串 转成 二进制 “10011100000000011100011111111101”    

/// </summary>  

/// <param name="s"></param>    

/// <returns></returns>    

public static string bianma(string s)     {        

byte[] data = Encoding.Unicode.GetBytes(s);        

StringBuilder result = new StringBuilder(data.Length * 8);

        foreach (byte b in data)        

  {            

    result.Append(Convert.ToString(b, 2).PadLeft(8, '0'));        

  }        

return result.ToString();    

}    

/// <summary>    

/// 将二进制 “10011100000000011100011111111101” 转成 字符串    

/// </summary>    

/// <param name="s"></param>    

/// <returns></returns>    

public static string jiema(string s)     {        

System.Text.RegularExpressions.CaptureCollection cs =  System.Text.RegularExpressions.Regex.Match(s, @"([01]{8})+").Groups[1].Captures;        

byte[] data = new byte[cs.Count];        

for (int i = 0; i < cs.Count; i++)        

{            

  data[i] = Convert.ToByte(cs[i].Value, 2);        

}        

return Encoding.Unicode.GetString(data, 0, data.Length);    

}

 

/// <summary>          

/// <函数:Decode>          

///作用:将16进制数据编码转化为字符串        

/// </summary>           

/// <param name="strDecode"></param>           

/// <returns></returns>           

public string DecodeToString(string strDecode)        

{            

string sResult = "";            

for (int i = 0; i < strDecode.Length / 4; i++)            

{                

  sResult += int.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);            

}            

  return sResult;        

}

/// <summary>        

/// 将16进制数转为10进制数,并保留小数位数        

/// </summary>        

/// <param name="strDecode"></param>        

/// <param name="i"></param>        

/// <returns></returns>        

public float DecodeToFloat(string strDecode, int i)        

{            

  int num = Convert.ToInt32(strDecode, 16);            

  float result = (float.Parse(num.ToString())) / i;            

  return result;        

}

/// <summary>        

/// 将16进制数转为10进制数整型        

/// </summary>        

/// <param name="strDecode"></param>        

/// <returns></returns>        

public int DecodeToInt(string strDecode)        

{            

  int result = Convert.ToInt32(strDecode, 16);            

  return result;        

}

/// <summary>        

/// 获取数据源(读取记事本)   

/// </summary>        

/// <returns></returns>        

public string GetData()        

{            

string effectiveData = "0";//有效数据            

try            

{                

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);                

StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));                

string strText = "";                

while (true){                    

string strTemp = sr.ReadLine();                    

if (strTemp != null)                    

{                        

strText += strTemp;                        

continue;                    

}                    

else                    

{                        

break;                    

}

               

}                

strText = strText.Trim();                

sr.Close();                

fs.Close();                        

return effectiveData;        

}

   /// <summary>
   /// 清空记事本内容
   /// </summary>
        public void ClearText()
        {
            //StreamWriter sw = new StreamWriter(filePath, false);
            //sw.Write(string.Empty);
            //sw.Close();
            //sw.Dispose();
            FileStream fs = new FileStream(filePath, FileMode.Create);
            fs.Close();
        }

转载于:https://www.cnblogs.com/yuhanzhong/p/3181264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值