码迷,mamicode.com
首页 > Windows程序 > 详细

C# 中byte[]数据转化相关操作

时间:2018-07-20 15:22:48      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:ddd   ring   col   tco   之间   efault   搜索   new   nbsp   

一、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# 中byte[]数据转化相关操作

标签:ddd   ring   col   tco   之间   efault   搜索   new   nbsp   

原文地址:https://www.cnblogs.com/jasminek/p/9341448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!