when people do...
#define MENUITEM1 0x0401
this is really an integer right?
so why don't people just do...
#define MENUITEM1 101
when people do...
#define MENUITEM1 0x0401
this is really an integer right?
so why don't people just do...
#define MENUITEM1 101
0x0401 is a hexadecimal number (1,025 in decimal)
I can't think of a good reason in that case to use hex, but if you wanted to define, for example, a bitmask such as:
Code:#define MASK 0xFFFF //1111 1111 1111 1111 binary //as opposed to #define MASK 65535
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
Who knows. Maybe it just looks 1337. Maybe it's stupid repellant: like,
#define MENUITEM1 101
J. Random Luser: "Oh look! A number! I'll just change it so my hack works."
#define MENUITEM 0x0401
J. Random Luser: "Oh crap... that must do something important. Better not mess with it. "
*original programmer smiles*
Or maybe it was used in context with other hex numbers where it would suck if you changed bases randomly via macro.
Certain features of the Win32 API, for example, requires you to use values in a certain range. Often, these ranges are given in hex numbers.
of course the best one is
Code:#define SOME_NUM 0x05 // dec 5 == hex 5, it's the same freakin number!!!!
although as citizen says, if you have a bunch of other valid hex values it's easier to keep consistency...
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
Nah, sometimes we need more action:
Code:#define JAMES_BOND 007
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
yes, but macros are EVIL! look, I'll prove it!Originally Posted by laserlight
Code:#define MACRO_OF_THE_BEAST 0x29A // no points for guessing this in decimal
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
I personally go with
Code:#define QUESTION 2B || !2B
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I use hex in my constants for the simple reason that I am decoding a binary file and over half the numbers I am working with are hex and it is easier to keep track of them in my head if I am reading them all in hex than to try and read some in decimal some in hex...
what is the conversion formula from hex to int (just wandering cuzz I just use random hex numbers in my code)
You seem a mite confused about hex.Originally Posted by Raigne
Google some on it, things like "number system", "hex to decimal tutorial", etc.
Here's a link to start with.
Basically, decimal, the system you use, is base 10. 10 symbols (the numbers 0 to 9) per digit in the number. Hexadecimal is base 16, so 16 symbols per digit in the number, represented by the numbers 0-9 and letters A-F. The above link will show you how to convert between the two.
Hexadecimal, binary, and decimal (among others) are all different ways of representing the same values.
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Conversion from hex to integer does not make sense. Hex is not a data type but a numbering system.
Binary - base 2
Octal - base 8
Decimal - base 10
Hex - base 16
So hex, binary, decimal, octal all have integers, just represented differently.
so really the compiler distinguishes the macro definition as an integer anyway... decimal would be good for a menu_id?
#define MENU1 101
#define MENU2 102
#define MENU3 103
or is it just as easy saying ...
#define MENU1 0x01
#define MENU2 0x02
#define MENU3 0x03
ORRRRRRR does it just depend on programming styles of each programmer?
101 and 0x01 are not the same... 101 and 0x65 are the same... sometimes it is easier to work with hex numbers, but most of the time you are better off with decimal
First, if you are writing this you need to decide if the macro is truly necessary, because it probably isn't.
Even if it is necessary, write your constant in whatever base you want as long as you don't confuse anyone who will read your code, and be consistant with the base you choose. Don't define a hex number and then use it with decimals.Code:const int MENUITEM1 = 1; const int MENUITEM2 = 2; const int MENUITEMn = n; ...