顯示具有 轉換 標籤的文章。 顯示所有文章
顯示具有 轉換 標籤的文章。 顯示所有文章

2010年12月31日 星期五

十六進位轉換

// Should be "7B"
String str = 123.ToString("X");

// Should be 291
int num = int.Parse("123", System.Globalization.NumberStyles.HexNumber);

//or
string hex = "ABC";
int myInt = Convert.ToInt32(hex, 16);

//VB
int x = &HFF

//C#
int x = 0xFF;

DateTime與字串互轉

字串轉DateTime
String MyString;
MyString = "1999-09-01 21:34 PM";

DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt",null);

DateTime轉字串
MyDateTime = new DateTime(1999, 09, 01, 21, 34, 00);
String MyString;
MyString = MyDateTime.ToString("yyyy-MM-dd HH:mm tt");
//將時間轉成微秒
MyDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")

格式化字串參考
//Format String For Dates
Your format string is your most important key. In most of my projects I make it a constant and then refer to the constant value in my code.

The following is the most communally used format characters:

d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.

f,ff,fff,ffff,fffff,ffffff,fffffff - Fraction of a second. The more F's the higher the presision.

h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero.
mm - Minutes with leading zero.

M - Numeric month with no leading zero.
MM - Numeric month with a leading zero.
MMM - Abbreviated name of month.
MMMM - Full month name.

s - Seconds with no leading zero.
ss - Seconds with leading zero.

t - AM/PM but only the first letter.
tt - AM/PM

y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.

zz - Time zone off set with +/-.

字元轉為數字

char c = '1';
int i;
i = Convert.ToInt32(c);

Enum轉換

//從字串轉換為Enum
YourEnum foo = (YourEnum) Enum.Parse(typeof(yourEnum), yourString);

//從數字轉換為Enum
YourEnum foo = (YourEnum)yourInt;

//將Enum轉為數字
enum Color
{
Green,
Yellow,
Red
}
Color t1 = Color.Green;
Console.WriteLine("{0},\t{1}",   t1, (int)t1);

字串轉為URL格式

using System.Web;
using System.Web.HttpUtility.UrlEncode;

Server.urlEncode(strURL);

2010年12月30日 星期四

字串與Byte陣列之轉換

字串轉為Byte陣列
byte[] byteArray = System.Text .Encoding.Default.GetBytes ( str );

Byte陣列轉為字串
byte [] dBytes = { 1, 2, 3, 4, 5}
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);

取小數點後兩位

Double sizemb;
sizemb.ToString("#.#0");