C#与unity3D通信 字节数组转为字符串 5
服务器发送:stringmessge;messge="111";vardate=ASCIIEncoding.UTF8.GetBytes(messge);socket.Se...
服务器发送: string messge;
messge = "111";
var date = ASCIIEncoding.UTF8.GetBytes(messge);
socket.Send(date); //正常发送
客户端接受: byte[] bt = new byte[1024];
int messgeLength = tcpSocket.Receive(bt);
string str = System.Text.Encoding.ASCII.GetString(bt);
Debug.Log(str); //正常接收并打印
if(String.Equals(str, "111")) //判等异常!
liansuo = true;
想问下 System.Text.Encoding.ASCII.GetString是将字节数组转换为字符串啊,为什么不能判等? 展开
messge = "111";
var date = ASCIIEncoding.UTF8.GetBytes(messge);
socket.Send(date); //正常发送
客户端接受: byte[] bt = new byte[1024];
int messgeLength = tcpSocket.Receive(bt);
string str = System.Text.Encoding.ASCII.GetString(bt);
Debug.Log(str); //正常接收并打印
if(String.Equals(str, "111")) //判等异常!
liansuo = true;
想问下 System.Text.Encoding.ASCII.GetString是将字节数组转换为字符串啊,为什么不能判等? 展开
展开全部
有两张方法:
方法一:
//字符串转byte
string StringMessage = "How Are you?";
Console.WriteLine("{0}", StringMessage);
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
Byte[] BytesMessage = ASCII.GetBytes(StringMessage);
//byte转字符串
Byte[] BytesMessage;
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
String StringMessage = ASCII.GetString( BytesMessage );
方法二:
//字符串转UTF-8 byte
string StringMessage = "Hello World How Are you? Pi /u03C0 Yen /uFFE5";
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
Byte[] BytesMessage = UTF8.GetBytes(StringMessage);
//UTF-8 byte 转字符串
Byte[] BytesMessage;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
String StringMessage = UTF8.GetString( BytesMessage );
方法一:
//字符串转byte
string StringMessage = "How Are you?";
Console.WriteLine("{0}", StringMessage);
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
Byte[] BytesMessage = ASCII.GetBytes(StringMessage);
//byte转字符串
Byte[] BytesMessage;
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
String StringMessage = ASCII.GetString( BytesMessage );
方法二:
//字符串转UTF-8 byte
string StringMessage = "Hello World How Are you? Pi /u03C0 Yen /uFFE5";
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
Byte[] BytesMessage = UTF8.GetBytes(StringMessage);
//UTF-8 byte 转字符串
Byte[] BytesMessage;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
String StringMessage = UTF8.GetString( BytesMessage );
追问
这几种方法我都尝试了,但判等的时候都不相等,不知道为什么?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询