M
Markus Freitag
Guest
Hello,
I receive a byte array and have to convert it into a string.
I only need one character in my string.
How can I reach my goal?
resToClient = (char)2 + "PING" + (char)4 + "WORK" + (char)4 + "ACK" + (char)3;
//Current Output:
Encoding.UTF8.GetString(buffer, 0, sizeReceive) "\u0002PING\u0004WORK\u0003"
Encoding.ASCII.GetString(new byte[]{ 2 }); "\u0002" string
//Should be:
For ASCII 2 (STX) 3 (ETX) 4 (EOT) I need only one character inside my string not more.
Thanks in advance for your help.
Greetings Markus.
My attempts were.
var buffer = new byte[10000];
sizeReceive = tcpClient.Client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
receiveBuffer += Encoding.UTF8.GetString(buffer, 0, sizeReceive).Replace('\u0002', '2').Replace('\u0003', '3').Replace('\u0004', '4');
byte[] chars = new byte[sizeReceive];
System.Buffer.BlockCopy(buffer, 0, chars, 0, sizeReceive);
//receiveBuffer += new string(chars);
foreach (var item in chars)
{
if (item < 30)
{
int t = (char)item;
receiveBuffer += (char)t;
}
else
receiveBuffer += Convert.ToChar(item);
}
Continue reading...
I receive a byte array and have to convert it into a string.
I only need one character in my string.
How can I reach my goal?
resToClient = (char)2 + "PING" + (char)4 + "WORK" + (char)4 + "ACK" + (char)3;
//Current Output:
Encoding.UTF8.GetString(buffer, 0, sizeReceive) "\u0002PING\u0004WORK\u0003"
Encoding.ASCII.GetString(new byte[]{ 2 }); "\u0002" string
//Should be:
For ASCII 2 (STX) 3 (ETX) 4 (EOT) I need only one character inside my string not more.
Thanks in advance for your help.
Greetings Markus.
My attempts were.
var buffer = new byte[10000];
sizeReceive = tcpClient.Client.Receive(buffer, 0, buffer.Length, SocketFlags.None);
receiveBuffer += Encoding.UTF8.GetString(buffer, 0, sizeReceive).Replace('\u0002', '2').Replace('\u0003', '3').Replace('\u0004', '4');
byte[] chars = new byte[sizeReceive];
System.Buffer.BlockCopy(buffer, 0, chars, 0, sizeReceive);
//receiveBuffer += new string(chars);
foreach (var item in chars)
{
if (item < 30)
{
int t = (char)item;
receiveBuffer += (char)t;
}
else
receiveBuffer += Convert.ToChar(item);
}
Continue reading...