摘要:本文主要向大家介绍了C#编程之C# 中byte[]数据转化相关操作,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之C# 中byte[]数据转化相关操作,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
一、int、float、double转byte[]
均使用System.BitConverter.GetBytes()。
1 int iii = 1;
2 float fff = 1234.12346f;
3 double ddd = 12.1264567;
4
5 byte[] b1,b2 = new byte[4];
6 byte[] b3 = new byte[8];
7 b1 = BitConverter.GetBytes(iii);
8 b2 = BitConverter.GetBytes(fff);
9 b3 = BitConverter.GetBytes(ddd);
二、string转byte[]
1 string str1 = "11111111";
2 byte[] b4 = Encoding.Default.GetBytes(str1);
三、byte[]转int、float、double、string
还是用BitConverter
byte[] b5 = new byte[4] { 0x01, 0x02, 0x03, 0x04 };
byte[] b6 = new byte[8] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; int i2 = BitConverter.ToInt32(b5, 0);
float f2 = BitConverter.ToSingle(b5, 0);
double d2 = BitConverter.ToDouble(b6, 0);
string str2 = BitConverter.ToString(b6, 0);
四、两个byte[]之间截取、复制
用System.Array可以完成对数组的创建、搜索、复制等操作
1 byte[] b7 = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
2 byte[] b8 = new byte[2];
3 Array.Copy(b7, 2, b8, 0,2);
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号