I'm trying to convert a string
to a byte[]
using the ASCIIEncoder
object in the .NET library. The string
will never contain non-ASCII characters, but it will usually have a length greater than 16. My code looks like the following:
public static byte[] Encode(string packet)
{
ASCIIEncoder enc = new ASCIIEncoder();
byte[] byteArray = enc.GetBytes(packet);
return byteArray;
}
By the end of the method, the byte array should be full of packet.Length
number of bytes, but Intellisense tells me that all bytes after byteArray[15]
are literally questions marks that cannot be observed. I used Wireshark to view byteArray
after I sent it and it was received on the other side fine, but the end device did not follow the instructions encoded in byteArray
. I'm wondering if this has anything to do with Intellisense not being able to display all elements in byteArray
, or if my packet is completely wrong.
If your packet
string basically contains characters in the range 0-255, then ASCIIEncoding
is not what you should be using. ASCII only defines character codes 0-127; anything in the range 128-255 will get turned into question marks (as you have observed) because there characters are not defined in ASCII.
Consider using a method like this to convert the string to a byte array. (This assumes that the ordinal value of each character is in the range 0-255 and that the ordinal value is what you want.)
public static byte[] ToOrdinalByteArray(this string str)
{
if (str == null) { throw new ArgumentNullException("str"); }
var bytes = new byte[str.Length];
for (int i = 0; i < str.Length; ++i) {
// Wrapping the cast in checked() will trigger an OverflowException
// if the character being converted is out of range for a byte.
bytes[i] = checked((byte)str[i]);
}
return bytes;
}
The Encoding
class hierarchy is specifically designed for handling text. What you have here doesn't seem to be text, so you should avoid using these classes.