研究了c#中資料型別之間的轉換涉及的一些問題,希望大家給予意見!
1、資料型別的類名
這裡講的資料的類名指的是: sytem.data.dbtype對應的型別,我是這樣理解的。
類名 system中相對應的型別
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 個位元組)
**如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace typetest
public partial class form1 : form
public form1()
initializecomponent();
private void form1_load(object sender, eventargs e)
bool bo = true;
byte b = 9;
char c = 'a';
sbyte sb = 8;
short s = 8;
int i= 7;
uint u = 6;
long l = 5;
this.textbox1.text = "typetest";
//其實型別就不寫上,自己可以真接加上去!
結果可看到:
typetestbool -> system.boolean
byte -> system.byte
char -> system.char
sbyte -> system.sbyte
short -> system.int16
int -> system.int32
uint -> system.uint32
long -> system.int64
說明,以後所編的**都是寫在private void form1_load(object sender, eventargs e)中的
2、value type間的轉換。
bool bo = true;
byte b = 9;
char c = 'a';
sbyte sb = 8;
short s = 8;
int i = 7;
uint u = 6;
long l = 5;
this.textbox1.text = "datatype";
此段**並沒有轉換資料型別,只說明它們的型別公別還是system.bool型…system.long型。
結果編譯報錯:
g:"projects"visual c#"convert"form1.cs(118): 無法將型別「int」隱式轉換為「short」
資料要進行強制轉換。
如上例修改如下:
就可以了!
short->byte
注意:溢位問題!
3、ascii<->unicode
char ch = 'a';
short ii = 65;
this.textbox1.text = "";
char name1 = '屈';
char name2 = '志';
short name3 = 21195;
the ascii code of 'a' is: 97
ascii is 65, the char is: a
the unicode of '屈' is: 23624
the unicode of '志' is: 24535
unicode is 21195, the name3 is: 勳
4、int<->string
float f = 12.3f;
string str = "258";
this.textbox1.text = "";
if (int.parse(str) == 258) //string->int
else
5、string<->char
string str = "quzhixun";
char chars = str.tochararray();//string->char
this.textbox1.text = "";
char name = ;
string sname = new string(name);//char->string
6、string<->byte
string s = "hi,屈志勳";
byte b1 = system.text.encoding.default.getbytes(s);//sting->byte,半個英文1個位元組,漢字2 個位元組。
byte b2 = system.text.encoding.unicode.getbytes(s); //sting->byte,都是兩個位元組。
string t1 = "", t2 = "";
foreach (byte b in b1)
t1 += b.tostring("") + " ";
foreach (byte b in b2)
t2 += b.tostring("") + " ";
this.textbox1.text = "";
byte b = ;
string s = system.text.encoding.ascii.getstring(b);//byte->string
7、轉換十六進製制
int a = 159357;
this.textbox1.text = "";
8、datetime<->long
double doubledate = datetime.now.tooadate();//按原來的double值輸出,datetime->long
datetime thedate = datetime.fromoadate(doubledate);//從原來的的double值獲得system.datetime物件,long->datetime
this.textbox1.text = "";
9、form datetime
datetime now = datetime.now;
string format;
this.textbox1.text = "";
format = """year"":yyyy,""month"":mm,""day"":dd hh:mm:ss";
format = "yy年m日d日";
gaopoadmin
vc資料型別轉換大全
int i 100 long l 2001 float f 300.2 double d 12345.119 char username 程佩君 char temp 200 char buf cstring str variant t v1 bstr t v2 一 其它資料型別轉換為字串 二 字串轉...
vc資料型別轉換大全
int i 100 long l 2001 float f 300.2 double d 12345.119 char username 程佩君 char temp 200 char buf cstring str variant t v1 bstr t v2 一 其它資料型別轉換為字串 二 字串轉...
Qt資料型別轉換大全
參考 qt中資料型別的轉換很常用,於是邊使用邊記錄,有不完整的,後邊再慢慢補充完善 qt中 int long轉換為qstring 有兩種方法 1 使用qstring number 如 long a 63 qstring s qstring number a,10 s 63 qstring t qst...