3

when I write a number to binary file, it won't display. but in case of a character, it does. why? how would you check to see if the file containing character is binary?

3
  • How are you viewing your data to determine how it appears? Commented Apr 23, 2010 at 0:49
  • For detecting whether a file is ASCII text or binary (with at least some level of confidence), see this question: stackoverflow.com/questions/277521/… Commented Apr 23, 2010 at 0:49
  • @Michael I'm using notepad to see it. Commented Apr 23, 2010 at 0:51

3 Answers 3

4

It has all to do with how you interpret what is in the file. Everything in a file is binary, a character an integer etc.

When you do TYPE in the console on a file (or CAT or whatever OS u have) the contents of the file are interpreted as text by default because the programmer of TYPE decided to write it like that.

When you write a program to read out data from a file it is up to you to decide how to interpret the data what you read.

That is why you can only guess file contents and that is why often the extension of a file is used to give a hint on how the contents should be interpreted.

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

4 Comments

so i'm seeing the ASCII characters because they are interpreted as characters (each 1 BYTE) by notepad. In case of an integer (int), its 4 BYTE in length.
@Gates127 - If you want to read your integer as text, you probably don't want to actually write your integer to a file - you want to write a text-representation of that integer to a file. That is (using your figures), rather than writing the value 12,345,678 (4 bytes), you want to write "12345678" (8 bytes).
I think i got the point, An integer is saved into 4 bytes and it maybe not exist in ASCII Table so the notepad couldn't interpret it. while an ASCII character has a range from 0 - 255.
Yep, that's pretty much it. :)
1

I think what you're really asking is whether you (personally) can interpret what's in the file.

As Anders alluded to, you can usually read a file as text regardless of what's in it - the characters, however, might make no sense.

Assuming you're writing software to perform this task, perhaps the following (high-level) algorithm will help:

  1. Create a list of characters you find acceptable as text
  2. Read the file, interpreting it as ASCII
  3. If any characters in the file aren't in your list, fail.
  4. Repeat steps 2 and 3 for any text-encoding you want to handle (ASCII, UTF-x, etc.)
  5. If nothing passes, it's not text.

Does this help?

Comments

1

Ok, I believe I can answer your question then. The reason a number is showing up as garbage in Notepad is because your saving an integer value - saving ascii code 0-9 which have funky characters associated with them. When saving a character, it will save the actual ascii value for that character (it treats the character as a number which translates to its ascii value) which will then show up as the character you would expect to see.

Another way to put it is :

int(1) = 1 

while

int('A') = 65

See how the they differ into the compiler?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.