string[] s1 = { "5", "168", "789", "28", "29", "155" };
string s2[] = {"abc","bcs"};
private byte[] byteBuffer;
public ClientState(Socket clntSock)
{
this.clntSock = clntSock;
rcvBuffer = new byte[BUFSIZE]; // Receive buffer
byteBuffer = new byte[strings.Length];
for (int i = 0; i < s1.Length; i++)
{
byteBuffer[i] = Byte.Parse(strings[i]);
}
}
string[] strings = { "5", "168", "23", "28", "29", "155" };
private const int BUFSIZE = 32; // Size of receive buffer
private byte[] rcvBuffer;
private Socket clntSock;
private byte[][] byteBuffer;
private NetworkStream netStream;
public ClientState(Socket clntSock)
{
this.clntSock = clntSock;
rcvBuffer = new byte[BUFSIZE]; // Receive buffer
byteBuffer = new byte[strings.Length][];
byteBuffer = ToBytes(strings);
}
static byte[][] ToBytes(string[] ascii)
{
byte[][] results = ascii.AsEnumerable().Select(x => Encoding.UTF8.GetBytes(x)).ToArray();
return results;
}
public ClientState(NetworkStream netStream, byte[][] byteBuffer)
{
this.netStream = netStream;
this.byteBuffer = byteBuffer;
}
public NetworkStream NetStream
{
get
{
return netStream;
}
}
public byte[][] BByteBuffer
{
set
{
byteBuffer = value;
}
get
{
return byteBuffer;
}
}
string[] strings = { "5", "168", "23", "28", "29", "155" };
private const int BUFSIZE = 32; // Size of receive buffer
private byte[] rcvBuffer;
private Socket clntSock;
private byte[][] byteBuffer;
private NetworkStream netStream;
public ClientState(Socket clntSock)
{
this.clntSock = clntSock;
rcvBuffer = new byte[BUFSIZE]; // Receive buffer
byteBuffer = new byte[strings.Length][];
byteBuffer = ToBytes(strings);
}
static byte[][] ToBytes(string[] ascii)
{
byte[][] results = ascii.AsEnumerable().Select(x => Encoding.UTF8.GetBytes(x)).ToArray();
return results;
}
public ClientState(NetworkStream netStream, byte[][] byteBuffer)
{
this.netStream = netStream;
this.byteBuffer = byteBuffer;
}
public NetworkStream NetStream
{
get
{
return netStream;
}
}
public byte[][] BByteBuffer
{
set
{
byteBuffer = value;
}
get
{
return byteBuffer;
}
}
public void ReceiveCallback(IAsyncResult asyncResult)
{
ClientState cs = (ClientState)asyncResult.AsyncState;
try
{
int recvMsgSize = cs.ClntSock.EndReceive(asyncResult);
if (recvMsgSize > 0)
{
Console.WriteLine("Thread {0} ({1}) - ReceiveCallback(): received {2} bytes",
Thread.CurrentThread.GetHashCode(),
Thread.CurrentThread.ThreadState,
recvMsgSize);
cs.ClntSock.BeginSend(cs.BByteBuffer, 0, cs.BByteBuffer.Length, SocketFlags.None,
new AsyncCallback(SendCallback), cs);
}
else
{
cs.ClntSock.Close();
}
}
catch (SocketException se)
{
Console.WriteLine(se.ErrorCode + ": " + se.Message);
cs.ClntSock.Close();
}
}
我正在将s1字符串数组转换为字节数组,然后将其发送到客户端。这是一台服务器。所以在客户端我想将字节数组转换回字符串数组。 使用Byte.Parse我无法转换字符串s2。那么如何将s1字符串数组和s2字符串数组转换为字节数组。
我想将字节数组转换回字符串数组,然后将结果放在客户端。
它表示无法从{[1}}
的byte [] []转换为byte []答案 0 :(得分:2)
你想要的字节数取决于你的编码。但它会是这样的:
string myString = "hello";
byte[] bytes = Encoding.UTF8.GetBytes(myString);
string fromBytes = Encoding.UTF8.GetString(bytes);
Console.Write(myString == fromBytes);
答案 1 :(得分:0)
试试这个
static void Main(string[] args)
{
string[] s1 = { "5", "168", "789", "28", "29", "155" };
byte[][] s1_byte = ToBytes(s1);
string[] s1_out = ToString(s1_byte);
DisplayBytes(s1_byte);
string[] s2 = { "sdf", "dsfds", "233" };
byte[][] s2_byte = ToBytes(s2);
DisplayBytes(s2_byte);
string[] s2_out = ToString(s2_byte);
}
static byte[][] ToBytes(string[] ascii)
{
byte[][] results = ascii.AsEnumerable().Select(x => Encoding.UTF8.GetBytes(x)).ToArray();
return results;
}
static void DisplayBytes(byte[][] bytes)
{
for (int i = 0; i < bytes.LongLength; i++)
{
Console.WriteLine(string.Join(" ", new List<byte>(bytes[i]).Select(x => "0x" + x.ToString("x2")).ToArray()));
}
}
static string[] ToString(byte[][] bytes)
{
List<string> results = new List<string>();
for (int i = 0; i < bytes.LongLength; i++)
{
string newString = Encoding.UTF8.GetString(bytes[i]);
results.Add(newString);
}
return results.ToArray();
}