Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String to byte array

String to byte array

Scheduled Pinned Locked Moved C#
data-structurestutorialquestionlounge
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    overfreeze
    wrote on last edited by
    #1

    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

    J M J 3 Replies Last reply
    0
    • O 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

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • O 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

        M Offline
        M Offline
        Mystic_
        wrote on last edited by
        #3

        similarly u can find the encoding of the string u recieve by System.Text.ASCIIEncoding.GetEncoding(recievedstring); and then pay back the sender in his coins :)

        1 Reply Last reply
        0
        • O 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

          J Offline
          J Offline
          Jun Du
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups