Magic numbers are unexplained values that often should be replaced with a named constant.
Questions tagged [magic-numbers]
193 questions
4
votes
1 answer
Magic number for plain text file
After googling, I found that Magic numbers can be used to identify the content type of a file.
In my program, I would like to validate the file content type on server side.
My client side code :
4
votes
3 answers
iPhone JPG image has non-standard magic bytes ff d8 ff e1?
My web app checks the first four bytes against the file extension before accepting uploaded images. A coworker showed me images from his iPhone which are being rejected. These have a different fourth byte (e1 = 225 instead of the expected e0 =…

Mike Kantor
- 1,400
- 4
- 24
- 45
4
votes
3 answers
Performance implications of using a variable versus a magic number
I'm often confused by this. I've always been taught to name numbers I use often using variables or constants, but if it reduces the efficiency of the program, should I still do it? Heres an example:
private int CenterText(Font font,…

Nathan
- 1,287
- 6
- 15
- 32
3
votes
2 answers
What is the problem with assigning magic numbers to named structure members?
Consider the following code:
const double Motor_Kv = 1.1e5; // Motor constant, [rpm/V]
const double Motor_R = 2e-2; // Winding resistance, [Ohm]
const double Motor_J = 3e-6; // Rotor angular mass, [kg*m2]
This has been refactored to use a…

Dmitry Grigoryev
- 3,156
- 1
- 25
- 53
3
votes
0 answers
How to solve "bad restore file magic number" when trying to load data?
I tried to load data to my R working directory and receive this error:
Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘classize.RData’ has magic number 'RDX3'
Use of save versions…

CMH
- 31
- 3
3
votes
1 answer
SonarQube: Magic numbers should not be used (squid:S109)
By definition,
"Magic number" is a value that should be given a symbolic name, but
was instead slipped into the code as a literal, usually in more than
one place.
This is a perfect example of a magic number
for(int i = 0; i < 4; i++){ …

Vamsidhar
- 822
- 11
- 24
3
votes
1 answer
Magic numbers in C++ implementation of Excel NORMDIST function
Whilst looking for a C++ implementation of Excel's NORMDIST (cumulative)
function I found this on a website:
static double normdist(double x, double mean, double standard_dev)
{
double res;
double x=(x - mean) / standard_dev;
if (x ==…

davetapley
- 17,000
- 12
- 60
- 86
3
votes
0 answers
Where do the magic numbers in this normal RNG come from?
A popular method to generate normally distributed numbers comes from the ratio of uniform deviates technique derived by Kinderman and Monahan and improved by Leva. The method is compact and requires little memory:
static Random r = new…

ruser9575ba6f
- 258
- 1
- 10
3
votes
1 answer
Java Constants across packages
I have a project that consists of 5 different packages in Java. All of them contain classes that have magic numbers and hardcoded strings for which I'm looking to create constants. I would like to know what is best practice. Should I create one…

Kyle Peters
- 45
- 3
3
votes
7 answers
Many Magic Numbers
I have a project where I transform Messages from one format into another.
The messages are identified by an id. I have a MappingTable with the id in the one format and the corresponding id in the other format.
For example:
2000 = 153
The id's are…

Bongo
- 2,933
- 5
- 36
- 67
3
votes
2 answers
Is there a way to remove magic numbers from a XML Schema?
I have a WSDL with some types defined. Some elements accept lists of elements and on the service return I also have elements with list of values (or other elements).
As a result I have some magic numbers in the XSD (e.g. minOccurs="10",…

tweety
- 31
- 2
3
votes
3 answers
PDF and DOCX Magic Numbers
I read the first byte to differentiate file types but both PDF and DOCX has a "0x50" magic number. How do I handle this circumstance?

user3770093
- 315
- 2
- 11
- 20
3
votes
2 answers
Set all values of an existing array to "magic numbers"
So I know that I can set each value of an array to a "magic number" (in this case a magic string) at the time of construction like;
string * myArray[] = {"foo", "bar", "baz"};
What would be useful is if I could declare my array;
string *…

Jekowl
- 317
- 2
- 12
3
votes
2 answers
Choosing a magic byte least likely to appear in real data
I hope this isn't too opinionated for SO; it may not have a good answer.
In a portion of a library I'm writing, I have a byte array that gets populated with values supplied by the user. These values might be of type Float, Double, Int (of different…

jberryman
- 16,334
- 5
- 42
- 83
3
votes
1 answer
How to validate image header in java
I've a web page that that can be used to upload files.
Now I need to check if the image file type is of correct type like png,jpg,jpeg,gif
I am using the mimeType that comes with the request ,but if i am loading the .txt file that was renamed to…

Uday Konduru
- 203
- 1
- 4
- 16