开发者

Check if file data is binary

开发者 https://www.devze.com 2022-12-27 17:14 出处:网络
when I write a number to binary file, it 开发者_JS百科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?It has all to do w

when I write a number to binary file, it 开发者_JS百科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?


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.


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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消