using System;
using System.Collections.Generic;
using System.Text;
namespace 类型转换
{
class Program
{
/*=========================格式化日期型数据===================
* 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 秒。一位数的秒数有一个前导零。
==================================================================*/
private void TestDateTimeToString()
{
DateTime now = DateTime.Now;
string format = "yyyy-MM-dd HH:mm:ss";
Console.WriteLine(format + ": " + now.ToString(format));
format = "yyyy年M月d日";
Console.WriteLine(format + ": " + now.ToString(format));
format = "yyyy-MM-dd";
Console.WriteLine(format + ": " + now.ToString(format));
format = "/"year/": yyyy, /'month/': MM, /'day/': dd";
Console.WriteLine(format + ": " + now.ToString(format) + "/n");
}
/*=================================================================*/
private void TestDateTimeLong()
{
double doubleDate = DateTime.Now.ToOADate();
DateTime theDate = DateTime.FromOADate(doubleDate);
Console.WriteLine("Double value of now: " + doubleDate.ToString());
Console.WriteLine("DateTime from double value: " + theDate.ToString());
/*
* 日期型数据,在 C# 中的参与运算的时候,应该也是转换为长整型数据来运算的。
* 它的长整型值是自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。
* 这个数在 C# 的 DateTime 中被称为 Ticks(刻度)。DateTime 类型有一个名为 Ticks 的长整型只读属性,
* 就保存着这个值。如此,要从一个 DataTime 型数据得到 long 型值就非常简单了,
* 只需要读出 DataTime 对象的 Ticks 值即可
*/
long longDate = DateTime.Now.Ticks;
DateTime Date = new DateTime(longDate);
Console.WriteLine("Ticks:"+Date.ToString("HH:mm:ss") + "/n");
}
/*==========字符串和字节数组之间的转换====字符串和字符数组之间的转换=========*/
private void TestStringBytes()
{
string s = "C#语言";
byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s);
string t1 = "", t2 = "";
foreach (byte b in b1)
{
t1 += b.ToString("") + " ";
}
foreach (byte b in b2)
{
t2 += b.ToString("") + " ";
}
Console.WriteLine("b1.Length = " + b1.Length );
Console.WriteLine(t1);
Console.WriteLine("b2.Length = " + b2.Length);
Console.WriteLine(t2);
byte[] bs = { 97, 98, 99, 100, 101, 102 };
string ss = System.Text.Encoding.ASCII.GetString(bs);
Console.WriteLine("The string is: " + ss + "/n");
}
private void TestStringChars()
{
string str = "mytest";
char[] chars = str.ToCharArray();
Console.WriteLine("Length of /"mytest/" is " + str.Length);
Console.WriteLine("Length of char array is " + chars.Length);
Console.WriteLine("char[2] = " + chars[2]);
char[] tcs = { 't', 'e', 's', 't', ' ', 'm', 'e' };
string tstr = new String(tcs);
Console.WriteLine("tstr = " + tstr);
}
/*==========================================================================*/
/*===============================基本数据类型===========================*/
/* bool -> System.Boolean (布尔型,其值为 true 或者 false)
char -> System.Char (字符型,占有两个字节,表示 1 个 Unicode 字符)
byte -> System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255)
sbyte -> System.SByte (带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127)
ushort -> System.UInt16 (无符号短整型,占 2 字节,表示 16 位正整数,范围 0 ~ 65,535)
uint -> System.UInt32 (无符号整型,占 4 字节,表示 32 位正整数,范围 0 ~ 4,294,967,295)
ulong -> System.UInt64 (无符号长整型,占 8 字节,表示 64 位正整数,范围 0 ~ 大约 10 的 20 次方)
short -> System.Int16 (短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767)
int -> System.Int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
long -> System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方)
float -> System.Single (单精度浮点型,占 4 个字节)
double -> System.Double (双精度浮点型,占 8 个字节)
==========================================================================*/
static void Main(string[] args)
{
DateTime Start = DateTime.Now;
Program ss = new Program();
ss.TestDateTimeToString();
ss.TestDateTimeLong();
ss.TestStringBytes();
ss.TestStringChars();
DateTime End = DateTime.Now;
/*===================获取时间间隔==================*/
TimeSpan span = End - Start;
Console.WriteLine(span.ToString()+"/n");
/*==================================================*/
/*====================进制转换==================*/
int num =123;
string Hexstr = Convert.ToString(num,16);
Console.WriteLine("Int To HexStr:"+Hexstr);
string hexstr = "7b";
int Num = Convert.ToInt32(hexstr, 16);
Console.WriteLine("HexStr to Int"+Num.ToString()+"/n");
/*============================================*/
/*===============C#的String.Format举例==============*/
string str1 = String.Format("{0:N1}", 56789); //result: 56,789.0
string str2 = String.Format("{0:N2}", 56789); //result: 56,789.00
string str3 = String.Format("{0:N3}", 56789); //result: 56,789.000
string str8 = String.Format("{0:F1}", 56789); //result: 56789.0
string str9 = String.Format("{0:F2}", 56789); //result: 56789.00
string str10 = String.Format("{0:X2}", 15);
string str11 = (56789 / 100.0).ToString("#.##"); //result: 567.89
string str12 = (56789 / 100).ToString("#.##"); //result: 567
Console.WriteLine(str10);
/*====================================================*/
/*===============FromBase64String的问题=====================
在使用Convert.ToBase64String()对字符串进行Base64编码时,注意的几点:
例:string s = "Hello";
byte[] bytes = Convert.FromBase64String(s);
以上代码在运行时会抛出FormatException异常.提示为:Base-64字符数组的无效长度
原因:当Convert.FromBase64String方法的参数s的长度小于 4 或不是 4 的偶数倍时,
* 将会抛出FormatException。
*/
try
{
byte[] bytes = Convert.FromBase64String("Hell"); // Normal.
Convert.FromBase64String("Hell "); // Normal.(忽略空格)
Convert.FromBase64String("Hello!"); // throw FormatException.
Convert.FromBase64String("Hello Net"); // Normal.(忽略空格)
Console.WriteLine(Encoding.Default.GetString(bytes));
}
catch (Exception g)
{
Console.WriteLine(g.Message);
}
/*==========================================================*/
Console.Read();
}
}
}