String to byte array
-
I'm working on a simple TCP chat program. The user should be able to send messages through a LAN. However, I can't find a way to convert the string to a byte array so I can send it with the NetworkStream.Write() method. Can anyone explain me how to do it and how to do the reverse process? Thanks in advance. overfreeze
-
I'm working on a simple TCP chat program. The user should be able to send messages through a LAN. However, I can't find a way to convert the string to a byte array so I can send it with the NetworkStream.Write() method. Can anyone explain me how to do it and how to do the reverse process? Thanks in advance. overfreeze
Look at the System.Text.UnicodeEncoding.Unicode.GetBytes() method. There are ASCII encodings in the System.Text namespace as well if you're using ASCII only text.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I'm working on a simple TCP chat program. The user should be able to send messages through a LAN. However, I can't find a way to convert the string to a byte array so I can send it with the NetworkStream.Write() method. Can anyone explain me how to do it and how to do the reverse process? Thanks in advance. overfreeze
-
I'm working on a simple TCP chat program. The user should be able to send messages through a LAN. However, I can't find a way to convert the string to a byte array so I can send it with the NetworkStream.Write() method. Can anyone explain me how to do it and how to do the reverse process? Thanks in advance. overfreeze
Here is some C++ code I have used for this purpose before. It should be easy to convert it to C#.
private: void Send(String \*msg) { try { // Create a client Int32 port = 30000; String \* server = S"127.0.0.1"; TcpClient \* client = new TcpClient(server, port); // Translate message into ASCII Byte data\[\] = System::Text::Encoding::ASCII->GetBytes(msg->ToString()); // Get a client stream for reading and writing NetworkStream\* stream = client->GetStream(); // Send the message to the connected client stream->Write(data, 0, data->Length); // Close the client client->Close(); } catch (Exception\* e) { MessageBox::Show(e->get\_Message(), "XLogoff Send Message Error"); } }
Best, Jun