I have recently started learning C# Networking and I was wondering how would you tell if the received Byte array is a file or a string?
-
2In either case it would be a byte array. *That's what network data is*. From there you have to parse the information yourself. – sircodesalot Feb 06 '13 at 20:23
-
That's something you have to determine one way or another. – Daniel Hedberg Feb 06 '13 at 20:24
-
Not quite a "file", an array contains data. You should loop through that array and write the data, `foreach(string data in array){ Console.WriteLine(data); }` – plast1K Feb 06 '13 at 20:25
5 Answers
A byte array is just a byte array. It's just got data in.
How you interpret that data is up to you. What's the difference between a text file and a string, for example?
Fundamentally, if your application needs to know how to interpret the data, you've got to put that into the protocol.

- 1,421,763
- 867
- 9,128
- 9,194
-
Telling the difference between a binary file (ex: *.ZIP) and ASCII or UTF8 encoded string is doable. Binary files can contain strings, but that doesn't mean you are not able to classify something as human readable or not with some level of certainty. – Louis Ricci Feb 06 '13 at 20:32
-
2@LastCoder `Really?\0` Now did I ***intend*** to send you the question "Really?" or did I ***intend*** send you the `long` value `17866430114522450`? – Scott Chamberlain Feb 06 '13 at 20:36
-
@LastCoder: There are heuristics, but I wouldn't want to depend on them... and I don't think it would be useful to introduce the OP to the concept right now. – Jon Skeet Feb 06 '13 at 20:40
-
@Scott Chamberlain - In the last line of my comment above I thought I made clear that there has to be some threshold of certainty that can be reached when classifying data. There's no absolutist classification. This is without even getting into the fact that most binary files have header signatures, while text files (or text or strings) do not have such headers. So I would classify your Really?\0 as an ASCII string with fairly good certainty as opposed to an 8 byte binary file. – Louis Ricci Feb 06 '13 at 20:47
-
@Jon Skeet - if the question was better phrased as "can I tell the difference between a binary file and a plain text file?" than simply File vs String the answers would be less fuzzy "you'll never know unless you decide beforehand" rubbish. Perhaps I was just over thinking the question (reforming it into something that made sense); taking the question completely literally, I'd agree bytes or bytes. – Louis Ricci Feb 06 '13 at 20:56
-
@LastCoder: I don't think they're "rubbish" at all - I think they're trying to express something that the OP really doesn't understand. I strongly suspect that you're approaching it from a far more nuanced level than would benefit the OP - it would be a much better idea for him to *know* the file type from the protocol than to start using heuristics. – Jon Skeet Feb 06 '13 at 21:19
A byte array is just a byte array. However, you could make the original byte array include a byte that describes what type it is (assuming you are the originator of it). Then you find this descriptor byte and use it to make decisions.

- 13,566
- 9
- 54
- 72
Strings are encoded byte arrays; files can contain strings and/or binary data.
ASCII strings use byte values between 0-127 to represent characters and control codes. For UTF8 people have written validation routines (https://stackoverflow.com/a/892443/884862).
You'd have to check the array for all of the string encoding characteristics before you could assume it's a binary file.
edit Here's an SO question about classifying a file type Using .NET, how can you find the mime type of a file based on the file signature not the extension using a signature (first X bytes) of the file to determine it's mimetype.

- 1
- 1

- 20,804
- 5
- 48
- 62
No you can't. Data is data, you must layer on top of your network communication form of protocol, it will need to say something like: "If the first byte I see is a 1
the next four bytes represent a int
, if I see a 2
read the next byte and that is the length of the text string that follows that..."
A much easier solution than inventing your own protocol is use a prebuilt one that gives you a higher level abstraction like WCF so you don't need to deal with byte arrays.

- 124,994
- 33
- 282
- 431
Not quite a "file", an array contains data. You should loop through that array and write the data,
Try this:
foreach(string data in array)
{
Console.WriteLine(data);
}
Now, if it doesn't contain strings, but data, you can simply use a
foreach(var data in array)
{
Console.WriteLine(data.ToString());
}

- 469
- 3
- 13