zoukankan      html  css  js  c++  java
  • C#常用数据类型间的转换

    数据类型有很多种,数据类型间的转换也是有很多的方法,如果不细心整理的话等到用的时候再查就会显得很浪费时间,所以决心整理出这篇博文。主要是讲解常用数据类型之间的转换方法以及常见数据类型所占字节数。

    字节数据类型与其它数据类型间的转换方法主要放在BitConverter类中:

     

    http://msdn.microsoft.com/zh-cn/library/system.bitconverter%28v=vs.100%29.aspx

    其它数据类型转换为字节类型主要通过重载GetBytes()方法返回字节类型数组。

     

    但是字符串类型根据编码方式的不同返回的字节类型不同,所以字符串类型的转换通过Encoding类中的GetBytes()方法返回字节数组。

    关于占用字节情况的注意:

    布尔类型占用一个字节,虽然布尔类型只用一比特就能表示但是还是需要占用一个字节,度娘说最小的存储单元是一字节。

    字符类型在C#中占用二字节,代表一个Unicode字符,Marshal.SizeOf((typeof(char)); 得到的是非托管类型的大小(占用一字节)。在测试中会发现存入数字就是一个字节,存入数组就是两个字节。

    还有不要误会啊,字符串类型是没有固定长度的,是在实际使用中根据实际情况确定长度的。

     

    下面测试代码:

    //定义变量并初始化
    byte[] Test_byte = new byte[] { 0, 1, 2, 3 };
    byte[] Temp_byte;
    bool Test_bool;
    int Test_int;
    float Test_float;
    char Test_char;
    string Test_string;
     
    //byte 2 bool
    //返回由字节数组中指定位置的一个字节转换来的布尔值。
    //public static bool ToBoolean(byte[] value, int startIndex);
    Test_bool = BitConverter.ToBoolean(Test_byte, 1);
    Console.WriteLine("bool is: {0}", Test_bool.ToString());
     
    //bool 2 byte
    //以字节数组的形式返回指定的布尔值。
    //public static byte[] GetBytes(bool value);
    Temp_byte = BitConverter.GetBytes(Test_bool);
    Console.WriteLine("bool length is: {0}", Temp_byte.Length);
     
    //byte 2 int
    //返回由字节数组中指定位置的四个字节转换来的 32 位有符号整数。
    //public static int ToInt32(byte[] value, int startIndex);
    Test_int = BitConverter.ToInt32(Test_byte, 0);
    Console.WriteLine("int is: {0}", Test_int);
     
    //int 2 byte
    //以字节数组的形式返回指定的 32 位有符号整数值。
    //public static byte[] GetBytes(int value);
    Temp_byte = BitConverter.GetBytes(Test_int);
    Console.WriteLine("int length is: {0}", Temp_byte.Length);
     
    //byte 2 float
    //返回由字节数组中指定位置的四个字节转换来的单精度浮点数。
    //public static float ToSingle(byte[] value, int startIndex);
    Test_float = BitConverter.ToSingle(Test_byte, 0);
    Console.WriteLine("float is: {0}", Test_float);
     
    //float 2 byte
    //以字节数组的形式返回指定的单精度浮点值。
    //public static byte[] GetBytes(float value);
    Temp_byte = BitConverter.GetBytes(Test_float);
    Console.WriteLine("float length is: {0}", Temp_byte.Length);
     
    //byte 2 char
    //返回由字节数组中指定位置的两个字节转换来的 Unicode 字符。
    //public static char ToChar(byte[] value, int startIndex);
    Test_char = BitConverter.ToChar(Test_byte, 0);
    Console.WriteLine("char is: {0}", Test_char);
     
    //char 2 byte
    //以字节数组的形式返回指定的 Unicode 字符值。
    //public static byte[] GetBytes(char value);
    Temp_byte = BitConverter.GetBytes(Test_char);
    Console.WriteLine("char length is: {0}", Temp_byte.Length);
     
    //byte 2 string
    //将指定的字节子数组的每个元素的数值转换为它的等效十六进制字符串表示形式。
    //public static string ToString(byte[] value, int startIndex);
    Test_string = BitConverter.ToString(Test_byte, 0);
    Console.WriteLine("string is: {0}", Test_string);
     
    //string 2 byte
    //在派生类中重写时,将指定的 System.String 中的所有字符编码为一个字节序列。
    //public virtual byte[] GetBytes(string s);
    Temp_byte = Encoding.Default.GetBytes(Test_string);
    Console.WriteLine("string length is: {0}", Temp_byte.Length);
    
    //char test
    char Test_c1 = '人';
    char Test_c2 = '1';
    Temp_byte = BitConverter.GetBytes(Test_c1);
    Console.WriteLine("Byte characters occupy is: {0}", Temp_byte.Length);
    Temp_byte = BitConverter.GetBytes(Test_c2);
    Console.WriteLine("Byte digital occupy is: {0}", Temp_byte.Length);
    
    //string test
    string Test_str1 = "人";
    string Test_str2 = "1";
    Temp_byte = Encoding.Default.GetBytes(Test_str1);
    Console.WriteLine("Byte characters occupy is: {0}", Temp_byte.Length);
    Temp_byte = Encoding.Default.GetBytes(Test_str2);
    Console.WriteLine("Byte digital occupy is: {0}", Temp_byte.Length);
    

    将一个基本数据类型转换为另一个基本数据类型Convert类:

     

    http://msdn.microsoft.com/zh-cn/library/system.convert_methods%28v=vs.100%29.aspx

    区别,以数值8为例:

    BitConverter.GetBytes();//以字节数组的形式返回指定的 32 位有符号整数值。

    返回的是字节数组,也就是数值33的存储形式:00000000 00000000 00000000 00001000 ,在调试中十进制显示:0 0 0 8

    Convert.ToByte();//将指定的 32 位有符号整数的值转换为等效的 8 位无符号整数。

    返回的是字节,也就是把数值8转换为字节类型的8了:00001000 ,在调试中十进制显示为:8

  • 相关阅读:
    python内建函数
    python中fromkeys的用法
    python中append和+号的区别
    python中input用法
    opencv3.2.0之绘图
    opencv3.2.0之Mat
    【学习】Python进行数据提取的方法总结【转载】
    【python】python中的enumerate()函数【笔记】
    【学习】如何安装GraphLab Create 【转载】
    【学习】python文件读写,用with open as的好处,非常好【转载】
  • 原文地址:https://www.cnblogs.com/zxlovenet/p/3562007.html
Copyright © 2011-2022 走看看