byte[] header = new byte[]{255, 216};
string ascii = Encoding.ASCII.GetString(header);
I expect ASCII to be equal to be FFD8 (JPEG SOI marker)
Instead I get "????"
byte[] header = new byte[]{255, 216};
string ascii = Encoding.ASCII.GetString(header);
I expect ASCII to be equal to be FFD8 (JPEG SOI marker)
Instead I get "????"
In this case you'd be better to compare the byte arrays rather than converting to string.
If you must convert to string, I suggest using the encoding Latin-1 aka ISO-8859-1 aka Code Page 28591 encoding, as this encoding will map all bytes with hex values are in the range 0-255 to the Unicode character with the same hex value - convenient for this scenario. Any of the following will get this encoding:
If you then wrote:
And expected "FFD8" to print out, that's not the way GetString work. For that, you would need:
Are you sure "????" is the result?
What is the result of:
On the other hand, pure ASCII is 0-127 only...
Yes, that's because ASCII is only 7-bit - it doesn't define any values above 127. Encodings typically decode unknown binary values to '?' (although this can be changed using DecoderFallback).
If you're about to mention "extended ASCII" I suspect you actually want
Encoding.Default
which is "the default code page for the operating system"... code page 1252 on most Western systems, I believe.What characters were you expecting?
EDIT: As per the accepted answer (I suspect the question was edited after I added my answer; I don't recall seeing anything about JPEG originally) you shouldn't convert binary data to text unless it's genuinely encoded text data. JPEG data is binary data - so you should be checking the actual bytes against the expected bytes.
Any time you convert arbitrary binary data (such as images, music or video) into text using a "plain" text encoding (such as ASCII, UTF-8 etc) you risk data loss. If you have to convert it to text, use Base64 which is nice and safe. If you just want to compare it with expected binary data, however, it's best not to convert it to text at all.
EDIT: Okay, here's a class to help image detection method for a given byte array. I haven't made it HTTP-specific; I'm not entirely sure whether you should really fetch the
InputStream
, read just a bit of it, and then fetch the stream again. I've ducked the issue by sticking to byte arrays :)I once wrote a custom encoder/decoder that encoded bytes 0-255 to unicode characters 0-255 and back again.
It was only really useful for using string functions on something that isn't actually a string.