文章目录
- int<->float
- [int<-> char](#int<-> char)
- int<->byte[]
- string<->int
- [string<->byte[ ]](#string<->byte[ ])
- string<->float
- string<->double
- [float<->byte[ ]](#float<->byte[ ])
- float<->double
- byte<->sbyte
int<->float
csharp
int->float
float f= 1.2345;
int i = (int)f*100
int<-> char
csharp
//int->char
char data=(char)0x55;
//char->int
int<->byte[]
csharp
//int -> byte[]
method1:
byte[] a = BitConverter.GetBytes(int_x);
method1:
b[0] = (byte)(u);
b[1] = (byte)(u >> 8);
b[2] = (byte)(u >> 16);
b[3] = (byte)(u >> 24);
//byte[] ->int
u = (uint)(b[0] | b[1] << 8 |b[2] << 16 | b[3] << 24);
string<->int
csharp
//int->string
int i=10;
string str=i.ToString();
//string->int
Int32.Parse(str);
string<->byte[ ]
csharp
//string->byte[ ]
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
//byte[ ]->string
string temp = System.Text.Encoding.Default.GetString(rbuf);
string<->float
csharp
//float->string
float f=12.3f;
string str=f.ToString();
//string->float
float float_x = float.Parse(DataX.Text);
string<->double
csharp
//double->string
double d=12.3333;
string str=f.ToString();
//string->double
double double_x = double.Parse(DataX.Text);
float<->byte[ ]
csharp
//float->byte[ ]
float float_x = float.Parse(DataX.Text);
byte[] a = BitConverter.GetBytes(float_x);
//byte[ ]->float
byte[] buffer = new byte[] {0x00,0x3a,0x36,0xe8};
Console.WriteLine(BitConverter.ToSingle(buffer,0).ToString());
float<->double
csharp
//float->double
data_float = (float)data_double;
//double->float
data_double = Convert.ToDouble(data_float);
byte<->sbyte
csharp
//byte->sbyte
byte value = 10;
convert.Tosbyte(value);
提示:几乎所有的类型都可以转为string类型。所以我们可以使用string类型作为中转,从而转化到其他类型