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#?
Keeping port feed in bytes or turning strings into bytes in C#?
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
- 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
- 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
- So far I'm not getting it to work.
- 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." - As in, I tried the above code and ASCII and GetDataFromPort() are not recognized.
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."- Ok, got it to work. After enough examples, could understand Copsey solution. Thanks
If you see an easier way to do this; please advise.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') + "] "; }- Edited bytfcsd Wednesday, November 04, 2009 10:40 PMedit

