Microsoft Developer Network > Forums Home > Visual Studio Express Editions Forums > Visual C# Express Edition > Keeping port feed in bytes or turning strings into bytes in C#?
Ask a questionAsk a question
 

AnswerKeeping port feed in bytes or turning strings into bytes in C#?

  • Tuesday, November 03, 2009 11:12 PMtfcsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have a string of bytes coming from a port and C# turns it into a string of gibberish. I need the feed from the port to remain in bytes or turn the string back into bytes. Tried bitconverter to no avail. Help?

Answers

  • Wednesday, November 04, 2009 12:21 AMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Is your data in Unicode?  If not, that would explain the issue.

    In that case, you probably need to convert your byte[] to a string and back using the appropriate Encoding class:

    Encoding ASCII = Encoding.ASCII; // My guess is that your bytes are probably in an ascii encoding, not unicode
    
    // Convert from byte[] to string -
    byte[] rawBytes = GetDataFromPort(); // Get your data into a byte[] array
    string dataAsString = ASCII.GetString(rawBytes); // Convert to a string properly
    
    // Convert from string to byte[] -
    byte[] convertedBytes = ASCII.GetBytes(dataAsString); // Converts from a string to ASCII encoded bytes
    
    


    For details, read Understanding Encodings.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked As Answer bytfcsd Wednesday, November 04, 2009 10:25 PM
    •  

All Replies

  • Wednesday, November 04, 2009 12:21 AMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Is your data in Unicode?  If not, that would explain the issue.

    In that case, you probably need to convert your byte[] to a string and back using the appropriate Encoding class:

    Encoding ASCII = Encoding.ASCII; // My guess is that your bytes are probably in an ascii encoding, not unicode
    
    // Convert from byte[] to string -
    byte[] rawBytes = GetDataFromPort(); // Get your data into a byte[] array
    string dataAsString = ASCII.GetString(rawBytes); // Convert to a string properly
    
    // Convert from string to byte[] -
    byte[] convertedBytes = ASCII.GetBytes(dataAsString); // Converts from a string to ASCII encoded bytes
    
    


    For details, read Understanding Encodings.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked As Answer bytfcsd Wednesday, November 04, 2009 10:25 PM
    •  
  • Wednesday, November 04, 2009 8:12 PMtfcsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    So far I'm not getting it to work.
  • Wednesday, November 04, 2009 8:25 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    We cannot see your screen.

    Please post the EXACT code that you are using. 

    Please post explain EXACTLY what "not getting it to work" means for your scenario.

    Please state any error messages or exceptions, and on which EXACT line of code they occur.

    Help someone to help you. 

    Mark the best replies as answers. "Fooling computers since 1971."
  • Wednesday, November 04, 2009 8:51 PMtfcsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    As in, I tried the above code and ASCII and GetDataFromPort() are not recognized.
  • Wednesday, November 04, 2009 9:03 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    As in, I tried the above code and ASCII and GetDataFromPort() are not recognized.

    *Gong*
    Please post your exact code.
    We know you are trying to use what Reed has.
    Maybe, just maybe, you have made a mistake somewhere. 
    Yours could actually be slightly different.
    Yours could actually be in the wrong place.
    Hmmm?

    In fact, if you used his example verbatim, it is unlikely it worked in your application without some changes.
    And please don't forget the other stuff I asked for.
    Help someone to help you.


    Mark the best replies as answers. "Fooling computers since 1971."
  • Wednesday, November 04, 2009 10:25 PMtfcsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Ok, got it to work. After enough examples, could understand Copsey solution. Thanks

    RxString2 = serialPort2.ReadExisting().ToString();
                Encoding ASCII = Encoding.ASCII;
                byte[] convertedBytes = ASCII.GetBytes(RxString2);
                //string dataAsString = ASCII.GetString(convertedBytes); 
                RxString2 = "";
                foreach (Byte b in convertedBytes)
                {
                    RxString2 += "[" + String.Format("{0:X}", b).PadLeft(2,'0')  + "] ";
                }
    
    If you see an easier way to do this; please advise.
    • Edited bytfcsd Wednesday, November 04, 2009 10:40 PMedit
    •