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();
}
C#二进制与字符串互转换,十六进制转换为字符串、float、int的更多相关文章
- SQL 行列转换数据转换为字符串
行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_cente ...
- C#DateTime.ToString 格式化时间字符串和数值类型转换为字符串
我们经常会遇到对时间进行转换,达到不同的显示效果,默认格式为:2006-6-6 14:33:34,如果要换成200606,06-2006,2006-6-6或更多的格式该怎么办呢?这里将要用到:Date ...
- python 字符串 大小写转换 以及一系列字符串操作技巧
总结 capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python" ...
- JS中将一个值转换为字符串的3种方法
1.value.toString() 2."" + value 3.String(value) 第一种方法存在的问题是,它不能把null和undefined转换为字符串.还有第二种 ...
- c#实现16进制和字符串之间转换的代码
以下示例演示如何执行下列任务: 获取字符串中每个字符的十六进制值. 获取与十六进制字符串中的每个值对应的字符. 将十六进制 string 转换为整型. 将十六进制 string 转换为浮点型. 将字节 ...
- python模块介绍- binascii:二进制和ASCII互转以及其他进制转换
20.1 binascii:二进制和ASCII互转作用:二进制和ASCII互相转换. Python版本:1.5及以后版本 binascii模块包含很多在二进制和ASCII编码的二进制表示转换的方法.通 ...
- Python十六进制与字符串的转换
电脑上装了Python2.7和3.3两个版本,平时运行程序包括在Eclipse里面调试都会使用2.7,但是由于某些原因在cmd命令行中输入python得到的解释器则是3.3, 一直没对此做处理,因为这 ...
- JAVA 十六进制与字符串的转换
public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式.如果参数为负,那么无符号整数值为参数加上 232:否则等于该参 ...
- java中json和字符串互转及日期转换 练习
一:以下是用到的jar名称: commons-beanutils-1.6.jar commons-collections-3.2.1.jar commons-lang-2.6.jar commons- ...
随机推荐
- Introduction to Mathematical Thinking - Week 9 评论答案2
根据 rubic 打分. 1. 我认为,如果说明 m, n 是自然数,所以最小值是 1 会更清楚.所以 Clarity 我给了 3 分.其他都是 4 分,所以一共是 23 分. 2. 我给出的分数 ...
- Exponential Backoff
f^x(f^y+f-m)+f-n =f^(x+y)+f^(f-m)+(f-n) <?php $exponent=0; w(80,3); function w($input,$base){ glo ...
- VSpy之C Code Interface的使用
Spy3 要运行 CCodeInterface 功能,需要安装运行环境,建议安装 Visual Studio2003,2005,2008,2010 或更新的版本.当然也可以安装 VC express ...
- PS导出@3x、@2x、@1x格式的iOS切图神器-Retinize
Retinize动作下载地址:http://retinize.it/ 使用:ps-载入动作-选中图片-执行动作
- 1.Configure the mongo Shell-官方文档摘录
Customize the Prompt 自定义提示 You may modify the content of the prompt by setting the variable prompt i ...
- hive批量执行sql命令及使用小技巧
root@hadoop-senior hive-0.13.1]$ bin/hive -helpusage: hive -d, --define <key=value> Variable s ...
- tfboys——tensorflow模块学习(四)
tensorflow功能函数 tf.abs 计算张量的绝对值 abs ( x , name = None ) 定义在:tensorflow/python/ops/math_ops.py. 参考指南:数 ...
- New Moto X 2014 全版本RSD&Fastboot刷官方底包教程
本来我是不想写教程的,因为这样的教程实在是太多了,基本上大家也都会了,为什么还要多次一举,发来发去的呢?实在没什么意义!但是我觉得吧,别人的教程写的都太过简单,太过明了了,有时候我们很难理解,这到底是 ...
- LeetCode:整数转罗马数字【12】
LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...
- 每天一个Linux命令(44)crontab命令
crontab命令被用来提交和管理用户需要周期性执行的任务,与windows下的计划任务类似. (1)用法: 用法: crontab [-u user] file cron ...