数据类型互转,字符串编码

1、字符串转byte数组

string msg="你好你好";

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);

2、byte数组转字符串

byte[] byts = File.ReadAllBytes(@"c:ooo.txt");
string msg = System.Text.Encoding.UTF8.GetString(byts);

3、字节数组转16进制文本表示

 string msg3= BitConverter.ToString(bytes);//结果:E5-93-88-E5-96-BD-E6-B2-83

4、字节数组转任何的基本类型。比如字节数组转int,single,double等

Single s= BitConverter.ToSingle(bytes, 4);

5、double类型数字转为计算机二进制存储的字节数组格式  互转

 byte[] bytes = BitConverter.GetBytes(38414.4);

double d=38414.4;

计算机二进制存储格式为:10000001110 0010110000011100110011001100110011001100110011001100  (红色符号位,蓝色阶码,黑色尾数)

                                  01000000 11100010 11000001 11001100 11001100 11001100 11001100 11001100  

                      10进制      64  226 193 204 204 204 204 204

                               

6、16进制转整型

int a=  Convert.ToInt32("AE", 16);

一、

类型   CTS类型 string int float double bool 转二进制存储    
整型 sbyte                  
  short Int16                
  int Int32                
  long Int64                
  byte Byte     Single s= BitConverter.ToSingle(bytes, 2);//从bytes数组第2位开始提取4个字节。进行转换          
  ushort UInt16                
  uint UInt32                
  ulong UInt64                
浮点 float Single           byte[] bytes= BitConverter.GetBytes(2.56);    
  double Double                
decimal decimal Decimal                
bool bool Boolean                
字符 char Char                
引用类型 string String                
                     
原文地址:https://www.cnblogs.com/crhdyl/p/4989142.html