C# 常用数据类型转换(二)

来源:互联网 发布:vb读取excel数据 编辑:程序博客网 时间:2024/04/30 03:43

6. 各种数值类型和字节数组之间的转换
数值类型转换为字节数组: System.BitConverter。该类提供了 byte[] GetBytes(…) 方法将各种数值类型转换成字节数组。
字节数组转换为数值类型: ToInt32、ToInt16、ToInt64、ToUInt32、ToSignle、ToBoolean 等方法将字节数组转换成相应的数值类型。
例子:

        static void Main(string[] args)        {            const string formatter = "{0,25}{1,30}{2,25}";            double aDoubl = 0.1111111111111111111;            float aSingl = 0.1111111111111111111F;            long aLong = 1111111111111111111;            int anInt = 1111111111;            short aShort = 11111;            char aChar = '*';            bool aBool = true;            Console.WriteLine(formatter, "argument", "byte array", "value");            Console.WriteLine(formatter, "--------", "----------", "-----");            // Convert values to Byte arrays and display them.            Console.WriteLine(formatter, aDoubl,                BitConverter.ToString(BitConverter.GetBytes(aDoubl)),      BitConverter.ToDouble(BitConverter.GetBytes(aDoubl),0));            Console.WriteLine(formatter, aSingl,                BitConverter.ToString(BitConverter.GetBytes(aSingl)), BitConverter.ToSingle(BitConverter.GetBytes(aSingl), 0));            Console.WriteLine(formatter, aLong,                BitConverter.ToString(BitConverter.GetBytes(aLong)), BitConverter.ToInt64(BitConverter.GetBytes(aLong), 0));            Console.WriteLine(formatter, anInt,                BitConverter.ToString(BitConverter.GetBytes(anInt)), BitConverter.ToInt32(BitConverter.GetBytes(anInt), 0));            Console.WriteLine(formatter, aShort,                BitConverter.ToString(BitConverter.GetBytes(aShort)), BitConverter.ToInt16(BitConverter.GetBytes(aShort), 0));            Console.WriteLine(formatter, aChar,                BitConverter.ToString(BitConverter.GetBytes(aChar)), BitConverter.ToChar(BitConverter.GetBytes(aChar), 0));            Console.WriteLine(formatter, aBool,                BitConverter.ToString(BitConverter.GetBytes(aBool)), BitConverter.ToBoolean(BitConverter.GetBytes(aBool), 0));            Console.ReadLine();        }

输出结果:
这里写图片描述

7. 转换成十六进制
任何数据在计算机内部都是以二进制保存的,所以进制与数据的存储无关,只与输入输出有关。所以,对于进制转换,我们只关心字符串中的结果。
使用 ToString(string) 方法加一些参数,就可以将其转换成十六进制。
这里需要一个 string 类型的参数,这就是格式说明符。十六进制的格式说明符是 “x” 或者 “X”,使用这两种格式说明符的区别主要在于 A-F 六个数字:”x” 代表 a-f 使用小写字母表示,而 “X” 而表示 A-F 使用大字字母表示。
有时为了显示结果的整齐,我们需要控制十六进制表示的长度,如果长度不够,用前导的 0 填补。解决这个问题,我们只需要在格式说明符“x”或者“X”后写上表示长度的数字就行了。
例子:

        static void Main(string[] args)        {            int val = 182;            Console.WriteLine("val(10) = " + val.ToString());            Console.WriteLine("val(16) = " + val.ToString("x"));            Console.WriteLine("val(16) = " + val.ToString("X"));            Console.WriteLine("val(16) = " + val.ToString("x4"));            Console.WriteLine("val(16) = " + val.ToString("X4"));            Console.ReadLine();        }

输出:
val(10) = 182
val(16) = b6
val(16) = B6
val(16) = 00b6
val(16) = 00B6

十六进制数的字符串转换成整型 需要借助于 Parse() 方法。这里,我需要 Parse(string, System.Globalization.NumberStyles) 方法。
第一个参数是表示十六进制数的字符串,如“AB”,“20”(表示十进制的 32) 等。
第二个参数 System.Globalization.NumberStyles 是一个枚举类型,用来表示十六进制的枚举值是 HexNumber。
例子:

        static void Main(string[] args)        {            int b = int.Parse("AB", System.Globalization.NumberStyles.HexNumber);            Console.WriteLine("b = " + b.ToString());            Console.ReadLine();        }

输出:
b = 171

8. 日期型数据和长整型数据之间的转换
日期型数据,在 C# 中的参与运算的时候,应该也是转换为长整型数据来运算的。它的长整型值是自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。这个数在 C# 的 DateTime 中被称为 Ticks(刻度)。DateTime 类型有一个名为 Ticks 的长整型只读属性,就保存着这个值。
DateTime 的构造函数中也提供了相应的,从长整型数据构造 DateTime 型数据的函数:DateTime(long)。
System.DateTime 提供了 double ToOADate() 和 static DateTime FromOADate(double) 两个函数也可以实现。
例子:

        static void Main(string[] args)        {            long longdate = DateTime.Now.Ticks;            DateTime theDate = new DateTime(longdate);            Console.WriteLine("longdate = " + longdate.ToString());            Console.WriteLine("theDate = " + theDate.ToString());            double doubleDate = DateTime.Now.ToOADate();            DateTime theDate1 = DateTime.FromOADate(doubleDate);            Console.WriteLine("doubleDate = " + doubleDate.ToString());            Console.WriteLine("theDate1 = " + theDate1.ToString());            Console.ReadLine();        }

输出:
这里写图片描述

9. 格式化日期型数据
编程的过程中,通常需要将日期型数据按照一定的格式输出,当然,输出结果肯定是字符串。为此,我们需要使用 System.DateTime 类的 ToString() 方法,并为其指定格式字符串。

MSDN 中,System.Globalization.DateTimeFormatInfo 类的概述里对模式字符串有非常详细的说明,因此,这里我只对常用的一些格式进行说明,首先请看下表:

d 月中的某一天 一位数的日期没有前导零
dd 月中的某一天 一位数的日期有一个前导零
ddd 周中某天的缩写名称 在 AbbreviatedDayNames 中定义
dddd 周中某天的完整名称 在 DayNames 中定义
M 月份数字 一位数的月份没有前导零
MM 月份数字 一位数的月份有一个前导零
MMM 月份的缩写名称 在 AbbreviatedMonthNames 中定义
MMMM 月份的完整名称 在 MonthNames 中定义
y 不包含纪元的年份 如果不包含纪元的年份小于 10,则显示不具有前导零的年份
yy 不包含纪元的年份 如果不包含纪元的年份小于 10,则显示具有前导零的年份
yyyy 包括纪元的四位数的年份
h 12 小时制的小时 一位数的小时数没有前导零
hh 12 小时制的小时 一位数的小时数有前导零
H 24 小时制的小时 一位数的小时数没有前导零
HH 24 小时制的小时 一位数的小时数有前导零
m 分钟 一位数的分钟数没有前导零
mm 分钟 一位数的分钟数有一个前导零
s 秒 一位数的秒数没有前导零
ss 秒 一位数的秒数有一个前导零

例子:

        static void Main(string[] args)        {            DateTime now = DateTime.Now;            string format = null;            format = "yyyy-MM-dd HH:mm:ss";            Console.WriteLine(format + ": " + now.ToString(format) + "\n");            format = "yy年M日d日";            Console.WriteLine(format + ": " + now.ToString(format) + "\n");            // 去除输出的文本信息中包含格式字符            format = "year: yyyy, month: MM, day: dd";            Console.WriteLine(now.ToString(format) + "\n");            format = "/'year/': yyyy, /'month/': MM, /'day/': dd";            Console.WriteLine(now.ToString(format).Replace("/", "") + "\n");            Console.ReadLine();        }

输出:
这里写图片描述

0 0
原创粉丝点击