How to get magic number of a binary file
Use libmagic from the file package to try and sniff out the type of file if that's your goal.
There are no general "magic" numbers in binary files on unix, though different formats might define their own. The above library knows about many of those and also use various other heuristics to try and figure out the format/type of file.
file <file_name>
magic numbers are usually stored in (linux):
/usr/share/file/magic
also check this link, someone was trying to use libmagic to get the information in C program, might be useful if you're writing something yourself.
Read this: http://linux.die.net/man/5/magic
It's complex, and depends on the specific file type you're looking for.
The unix file command uses magic number. see the file man page for more.(and where to find the magic file )