【C#学习】格式转化

文章目录

  • int<->float
  • int<-> char
  • int<->byte[]
  • string<->int
  • string<->byte[ ]
  • string<->float
  • string<->double
  • float<->byte[ ]
  • float<->double
  • byte<->sbyte

int<->float

int->float
float f= 1.2345;
int i = (int)f*100

int<-> char

//int->char
char data=(char)0x55;
//char->int

int<->byte[]

//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

//int->string
int i=10;
string str=i.ToString();
//string->int
Int32.Parse(str);

string<->byte[ ]

//string->byte[ ]
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
//byte[ ]->string
string temp = System.Text.Encoding.Default.GetString(rbuf);

string<->float

//float->string
float f=12.3f;
string str=f.ToString();
//string->float
float float_x = float.Parse(DataX.Text);

string<->double

//double->string
double d=12.3333;
string str=f.ToString();
//string->double
double double_x = double.Parse(DataX.Text);

float<->byte[ ]

//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

//float->double
data_float = (float)data_double;
//double->float
data_double = Convert.ToDouble(data_float);

byte<->sbyte

//byte->sbyte
byte value = 10;
convert.Tosbyte(value);

提示:几乎所有的类型都可以转为string类型。所以我们可以使用string类型作为中转,从而转化到其他类型

你可能感兴趣的:(编程相关技术,c#,学习,开发语言)