magic-numbers

How do I choose a good magic number for my file format?

大憨熊 提交于 2019-12-03 10:29:39
I am designing a binary file format from scratch, and I would like to include some magic bytes at the beginning so that it can be identified easily. How do I go about choosing which bytes? I am not aware of any central registry of magic numbers, so is it just a matter of picking something fairly random that isn't already identified by, say, the file command on a nearby UNIX box? Stay away from super-short magic numbers. Just because you're designing a binary format doesn't mean you can't use a text string for identifier. Follow that by an EOF char, and as an added bonus people who cat or type

Is -1 a magic number? An anti-pattern? A code smell? Quotes and guidelines from authorities [duplicate]

爷,独闯天下 提交于 2019-12-03 05:47:13
This question already has answers here : Constant abuse? (17 answers) Possible Duplicate: Constant abuse? I've seen -1 used in various APIs, most commonly when searching into a "collection" with zero-based indices, usually to indicate the "not found" index. This "works" because -1 is never a legal index to begin with. It seems that any negative number should work, but I think -1 is almost always used, as some sort of (unwritten?) convention. I would like to limit the scope to Java at least for now. My questions are: What are the official words from Sun regarding using -1 as a "special" return

Is 23,148,855,308,184,500 a magic number, or sheer chance?

人盡茶涼 提交于 2019-12-02 13:46:02
News reports such as this one indicate that the above number may have arisen as a programming bug. A man in the United States popped out to his local petrol station to buy a pack of cigarettes - only to find his card charged $23,148,855,308,184,500. That is $23 quadrillion (£14 quadrillion) - many times the US national debt.* In hex it's $523DC2E199EBB4 which doesn't appear terribly interesting at first sight. Anyone have any thoughts about what programming error would have caused this? Guffa Add the cents to the number and you get 2314885530818450000, which in hexadecimal is 2020 2020 2020

How do I get the size of a file in megabytes using Perl?

早过忘川 提交于 2019-11-29 03:45:11
I want to get the size of a file on disk in megabytes. Using the -s operator gives me the size in bytes, but I'm going to assume that then dividing this by a magic number is a bad idea: my $size_in_mb = (-s $fh) / (1024 * 1024); Should I just use a read-only variable to define 1024 or is there a programmatic way to obtain the amount of bytes in a kilobyte? EDIT: Updated the incorrect calculation. If you'd like to avoid magic numbers, try the CPAN module Number::Bytes::Human . use Number::Bytes::Human qw(format_bytes); my $size = format_bytes(-s $file); # 4.5M You could of course create a

What does the constant 0.0039215689 represent?

三世轮回 提交于 2019-11-28 02:44:15
I keep seeing this constant pop up in various graphics header files 0.0039215689 It seems to have something to do with color maybe? Here is the first hit on Google : void RDP_G_SETFOGCOLOR(void) { Gfx.FogColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f; Gfx.FogColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f; Gfx.FogColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f; Gfx.FogColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f; } void RDP_G_SETBLENDCOLOR(void) { Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f; Gfx.BlendColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f; Gfx.BlendColor.B = _SHIFTR(w1, 8, 8) * 0

What are “magic numbers” in computer programming?

天涯浪子 提交于 2019-11-27 23:28:52
When people talk about the use of "magic numbers" in computer programming, what do they mean? Magic numbers are any number in your code that isn't immediately obvious to someone with very little knowledge. For example, the following piece of code: sz = sz + 729; has a magic number in it and would be far better written as: sz = sz + CAPACITY_INCREMENT; Some extreme views state that you should never have any numbers in your code except -1, 0 and 1 but I prefer a somewhat less dogmatic view since I would instantly recognise 24, 1440, 86400, 3.1415, 2.71828 and 1.414 - it all depends on your

How to get magic number of a binary file

Deadly 提交于 2019-11-27 23:06:29
There is a magic number associated with each binary file , does anyone know how to retrieve this information from the 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

How do I get the size of a file in megabytes using Perl?

独自空忆成欢 提交于 2019-11-27 17:40:05
问题 I want to get the size of a file on disk in megabytes. Using the -s operator gives me the size in bytes, but I'm going to assume that then dividing this by a magic number is a bad idea: my $size_in_mb = (-s $fh) / (1024 * 1024); Should I just use a read-only variable to define 1024 or is there a programmatic way to obtain the amount of bytes in a kilobyte? EDIT: Updated the incorrect calculation. 回答1: If you'd like to avoid magic numbers, try the CPAN module Number::Bytes::Human. use Number:

The cause of “bad magic number” error when loading a workspace and how to avoid it?

十年热恋 提交于 2019-11-27 07:55:49
I tried to load my R workspace and received this error: Error: bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning message: file ‘WORKSPACE_Wedding_Weekend_September’ has magic number '#gets' Use of save versions prior to 2 is deprecated I'm not particularly interested in the technical details, but mostly in how I caused it and how I can prevent it in the future. Here's some notes on the situation: I'm running R 2.15.1 on a MacBook Pro running Windows XP on a bootcamp partition. There is something obviously wrong this workspace file, since it weighs in

What does the constant 0.0039215689 represent?

半腔热情 提交于 2019-11-27 05:00:42
问题 I keep seeing this constant pop up in various graphics header files 0.0039215689 It seems to have something to do with color maybe? Here is the first hit on Google: void RDP_G_SETFOGCOLOR(void) { Gfx.FogColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f; Gfx.FogColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f; Gfx.FogColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f; Gfx.FogColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f; } void RDP_G_SETBLENDCOLOR(void) { Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f;