C#与unity3D通信 字节数组转为字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#与unity3D通信 字节数组转为字符串相关的知识,希望对你有一定的参考价值。

服务器发送: 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是将字节数组转换为字符串啊,为什么不能判等?

有两张方法:
方法一:
//字符串转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 );追问

这几种方法我都尝试了,但判等的时候都不相等,不知道为什么?

参考技术A 这个问题你可以到paws3d上去看看,应该是有类似的案例的

C# 字节数组与字符串互相转换

string类型转成byte[]:

byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

 

byte[]转成string:

string str = System.Text.Encoding.Default.GetString ( byteArray );

 

string类型转成ASCII byte[]:

("01" 转成 byte[] = new byte[]{ 0x30,0x31})

byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );

 

ASCIIbyte[]转成string:

(byte[] = new byte[]{ 0x30, 0x31} 转成"01")

string str = System.Text.Encoding.ASCII.GetString ( byteArray );

 

byte[]转16进制格式string:

new byte[]{ 0x30, 0x31}转成"3031":

publicstaticstring ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF "

{string hexString = string.Empty;

if ( bytes != null )

{

StringBuilder strB = new StringBuilder ();

for ( int i = 0; i < bytes.Length; i++ )

{

strB.Append ( bytes[i].ToString ( "X2" ) );

}

hexString = strB.ToString ();

}return hexString;

}

 

16进制格式string 转byte[]:

publicstaticbyte[] GetBytes(string hexString, outint discarded)

{

discarded = 0;

string newString = "";

char c;// remove all none A-F, 0-9, charactersfor (int i=0; i<hexString.Length; i++)

{

c = hexString[i];if (IsHexDigit(c))

newString += c;

else

discarded++;

}// if odd number of characters, discard last characterif (newString.Length % 2 != 0){ discarded++;

newString = newString.Substring(0, newString.Length-1); }

int byteLength = newString.Length / 2;byte[] bytes = newbyte[byteLength];string hex;int j = 0;for (int i=0; i<bytes.Length; i++){

hex = new String(new Char[] {newString[j], newString[j+1]});

bytes[i] = HexToByte(hex); j = j+2;

}

return bytes;

}

以上是关于C#与unity3D通信 字节数组转为字符串的主要内容,如果未能解决你的问题,请参考以下文章

网络上的 C# 列表与字节数组效率

Java题目:编写程序,将一个字符串转为字节数组输入流。转换为大写字母输出。

C 字节数组转换成字符串

C# 字符串与字节数组相互转换

c#怎么把byte数组转换为字符串

C# 字节数组与字符串互相转换