locked
String to byte array RRS feed

  • Question

  • hi..
    i have some string data to send through serial COM port, therefore i need to convert string data into bytes for transfering... I tried Convert.ToByte( strdata ) even though its not working...
    HELP...
    Wednesday, March 15, 2006 8:44 AM

Answers

  • Hi!

    Change encoding if you want, but generally for strings this is the way:

     

    byte[] data = System.Text.Encoding.UTF8.GetBytes("sample string");

     

    Another (and little more expensive) way is to serialize data to the binary memory stream.

    Wednesday, March 15, 2006 8:52 AM
  • You can use the a specific encoding and convert the string with that encoding.


    string myString = "the string to convert.";
    byte[] stringInBytes = System.Text.Encoding.UTF7.GetBytes( myString );

     

    Wednesday, March 15, 2006 8:55 AM

All replies

  • Hi!

    Change encoding if you want, but generally for strings this is the way:

     

    byte[] data = System.Text.Encoding.UTF8.GetBytes("sample string");

     

    Another (and little more expensive) way is to serialize data to the binary memory stream.

    Wednesday, March 15, 2006 8:52 AM
  • You can use the a specific encoding and convert the string with that encoding.


    string myString = "the string to convert.";
    byte[] stringInBytes = System.Text.Encoding.UTF7.GetBytes( myString );

     

    Wednesday, March 15, 2006 8:55 AM