45

Once I asked a guy "what is the difference between ASCII and Binary files?"

And he said "Binary files always have \x00"

I've been searching about this and found What is the meaning of \x00 , \x04 in PHP

so the conclusion is, ASCII files don't have NULL character?

Community
  • 1
  • 1
Bagong21
  • 457
  • 1
  • 4
  • 9
  • 1
    That guy either didn't know what the he was talking about or you misunderstood him (that's not an XOR by the way). –  May 10 '11 at 18:28
  • The question should be: What is the difference between a [text file](https://en.wikipedia.org/wiki/Text_file) and a [binary file](https://en.wikipedia.org/wiki/Binary_file)? An ASCII file is a file containing only bytes with a decimal value in range 0 to 127. That can be a text file or a binary file. PHP is designed for processing __text__ files which can be also files containing characters not present in the [ASCII table](https://www.asciitable.com/) being [character encoded](https://en.wikipedia.org/wiki/Character_encoding) with Windows-1252 or UTF-8 or other encodings. – Mofi Aug 04 '23 at 05:14
  • __Text__ files do not contain null bytes in most character encodings. But text files Unicode encoded with [UTF-16](https://en.wikipedia.org/wiki/UTF-16) or [UCS-2](https://en.wikipedia.org/wiki/Universal_Coded_Character_Set) or [UTF-32](https://en.wikipedia.org/wiki/UTF-32) have lots of null __bytes__ which are not interpreted as null *character*. For a computer exists only 0s and 1s. How a sequence of 0s and 1s is interpreted by a program is defined by definitions, rules and standards. – Mofi Aug 04 '23 at 05:21

2 Answers2

26

An ASCII file might be read or interpreted as having NULL-terminated strings, carriage returns & line-feeds, or other control characters, that are intended to be read and acted on. For example, a text reader might look for a line of text, where a line is "however many characters you see before you get to a linefeed"

A binary file is considered to be just a sequence of bytes - none of them has any special meaning, in the sens that a text-reader would interpret them.

\x00 is an example of a specific byte value (HEX 0), that might be interpreted in a special way by a text reader.

jwismar
  • 12,164
  • 3
  • 32
  • 44
-2

Wrong. ASCII files have NULL characters. In fact, every string in ASCII ends at a NULL.

ASCII files are files that only contain ASCII characters x0 - x127.

Binary files contain data and each individual byte can be an ascii character, an integer, pointer, etc. Its just how to write data to the file and how you rad it back.

Aater Suleman
  • 2,286
  • 18
  • 11
  • I found this good website http://mark0.net/hexdump.html and I uploaded an ASCII file, but I didn't find any NULL character? or maybe the website needs revision :D thanks anyway – Bagong21 May 10 '11 at 18:49
  • Just coz that one file didnt have a null doesnt mean NULL is not a part of ASCII. It happens to be the FIRST ASCII character according to ASCII:) http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm – Aater Suleman May 10 '11 at 18:56
  • Good. Then I am confused. I do not understand how you conclude that ASCII files do not have NULL by uploading one file and looking at its content? – Aater Suleman May 10 '11 at 19:16
  • did you see http://stackoverflow.com/questions/1182812/what-is-the-meaning-of-x00-x04-in-php ? it's the link above, on this question – Bagong21 May 10 '11 at 19:40
  • 2
    I didn't conclude anything, I've searched and tested. did you do that? – Bagong21 May 10 '11 at 19:44
  • yes I checked. How does that imply that ascii files don't contain NULLs? Does it say that somewhere? – Aater Suleman May 10 '11 at 19:45
  • yup. tested, I can write and read an ascii file with a null in there. Hexdump shows the null if you write a null to a file, as expected. It just happens that the file you uploaded didn't have a null. You can write an ascii file using perl/C/python and put a null in there using \0. then open it using the editor of your choice to confirm it works and then upload it to hexdump. dude I studied computers for 10 years, i design them on a daily basis. they are simple devices, not magic. If null is a part of ascii, it can appear in an ascii file (by definition). – Aater Suleman May 10 '11 at 20:01
  • 19
    This is an old answer, but for the record: The generic statement that "ASCII files have NULL characters" is misleading, and the statement "every string in ASCII ends at a NULL" is just wrong. An ASCII file is just a file that contains only ASCII characters. It may or may not contain NULL. NULL has no specific meaning in an ASCII file, and it certainly doesn't act routinely as a termination marker. What is true is that some languages (e.g. C) use NULL to mark the end of a string in RAM. A programmer might decide to use this representation also in a file, but there's no obligation to do so. – Schmuddi Oct 20 '17 at 15:58