2

This is my piece of code (file is a HttpPostedFileBase type):

var imageStream = file.InputStream;
var header = new Byte[4];
imageStream.Read(header, 0, header.Length);

Now, while my code runs, i place a breakpoint, and in my immediate window i check values:

header
{byte[4]}
    [0]: 255
    [1]: 216
    [2]: 255
    [3]: 224

But, when i want to convert this byte array to string of ASCII, i get this (values obtained by immediate window):

Encoding.ASCII.GetString(header)
"????"
Encoding.ASCII.GetString(header, 0, 2)
"??"

What am I doing wrong?

1
  • 2
    ASCII = 7 bit encoding, therefore numbers>127 are not part of the ASCII character set. Commented Sep 10, 2012 at 15:46

3 Answers 3

5

Characters above 127 cannot be represented as ASCII (which is a 7-bit encoding), and are therefore turned into ?.

What are you trying to achieve? There are many other encodings which may be more suitable for what you're trying to do - or no encoding at all maybe. It is important to understand that a char is not equivalent to a byte.

If the header is a "fixed" 4-byte sequence, don't use characters but rather the bytes directly (or an integer representation - see the BitConverter class for converting byte arrays to other things).

Sign up to request clarification or add additional context in comments.

2 Comments

I want it to become a string, so i can use an .StartsWith() method on it.
@ojek, if you want it to be a string, you need to know what the mapping between the single bytes (8-bit) and the unicode charpoints (16-bit) are - oitherwise a string comparison doesn't work. That's why you need to pick the appropriate encoding, or avoid strings altogether and compare plain bytes.
3

ASCII character codes are in the range 0 to 127 inclusive. Your data is outside that range. Hence the attempt to interpret the data as ASCII fails. The ? characters are used to indicate characters that could not be decoded.

The first 4 bytes, in hex, are

FF D8 FF E0

That's the header for a JPEG file.

So, converting these bytes into a string is just not the appropriate course of action.

Comments

1

It is because these characters are not printable using ASCII - try using Encoding.UTF8.GetString(header) instead.

EDIT: As utilising UTF8 blindly seems inappropriate, you could perhaps try the following:- Request.ContentEncoding.GetString(header) which should utilise the encoding configured in the request.

8 Comments

What makes you think that those bytes are encoded in UTF8?
A fair point - but who says they arent? ;-)
Well, they aren't, are they? I have a particular distaste for trial and error programming. Which is what you are advocating.
It is a filestream, i have no idea how are they encoded.
I have just edited the answer - hope it helps
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.