1、byte 转 string
byte by = 0x01;
string st;
st = by.Tostring();
st = "1";
string st1 = “”;
st1 = by.tostring("X2");
st1 = "01";
byte by =0x41;
string st1 = “”;
st1 = ((char)by).tostring();
st1 = ‘A’;
2、byte[]转string
byte[] by = {0x1F,0xFF,0xFF,0xFF} ;
stirng st = "";
string st1 =”“;
st = BitConverter.ToString(by);
st = "1F-FF-FF-FF"; //中间的-无法取出,系统自带的
st1 = BitConverter.ToString(by,1,3);//从第一个位置,取3个字节长度
st1 = ”FF-FF-FF“;
3、single 转string
Single si = 55.5;
string st = "";
st = si.ToString("00.0");
st = "55.5";
4、string 转byte int32 single
字符串中截取
string st1 = “1515155”;
string st =“”;
string st2 = "";
st1 = st.Substring(0 ,2); //从0位置开始,截取2个字符
st2 = st.Substring(0 ,4); //从0位置开始,截取2个字符
st1 =“15”;
byte by;
by = Convert.ToByte(stemp, 16);
by = 0x15;
st2 ="1515";
int32 in =0;
in = Convert.ToInt32(st2, 16);
in = 0x1515 = 12425;
5、byte【】 按照ASCII转String
byte[] by = {0x31,0x32,0x33,0x34};
string st1 = “”;
st1 = System.Text.Encoding.ASCII.GetString(by);
st1 = "1234";
6、string 按照ASCII转byte[]
string st = “12345“;
byte[] by;
by = System.Text.Encoding.ASCII.GetBytes(st);
by ={0x31,0x32,0x33,0x34,0x35};
7、 string类型转成byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
byte[]转成string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
8、ASCIIbyte[]转成string:
(byte[] = new byte[]{ 0x30, 0x31} 转成"01")
string str = System.Text.Encoding.ASCII.GetString ( byteArray );