Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hexadecimal Numbers

Started by Bleakcabal Sep 24, 2000 at 2:27 PM 3 replies 350+ views
Original Post
Bleakcabal
Bleakcabal
Suppose I wish to pass a defined hexadecimal number which is passed to a function. What is the best parameter type for this job ? Here is an example of what I mean : #define DIK_1 0x02 #define DIK_9 0x0A #define DIK_0 0x0B Thank you in advance for any help.
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
MikeD
MikeD
Sure you can #define them and that''s a perfectly acceptable solution (look in some header files of, say, microsoft or standard libraries and you''ll find plenty of such endless #defines). However, one question. Why do you want to define them as Hex numbers? What difference does that make?
If you consider them standard, in order, numbers then you can enum them (starting with whatever value you choose). If they''re not in linear squence though then #define is the best to use.

But why Hex?

Mike
alexmoura
alexmoura
If I undestood what you want, it depends on the size of the hexadecimal numbers; for instance in the example you gave, all the hexadecimal numbers are 8 bit (a simple rule, for each two digits you get 8 bits), so a byte (char/unsigned char would do fine)
however, a hexadecimal number can be any size(for example, 0xFA0155C0 - 32 bit), just like a decimal number. You just have to see how large is the biggest number you're expecting to pass.

Now, if you wanted to know if you should use a reference or a pointer, I don't think you can (or at least should)pass a defined number to a function via reference or a pointer, for two reasons: first, a number is usually 32 bits or less, wich makes it faster to pass normally, Second, you'de be messing with memory addresses of memory that doesn't belong to variables, and you risk to be able to change the value of a #define, wich arranjes for some pretty weird errors.


You know, I never wanted to be a programmer...

Alexandre Moura

Edited by - alexmoura on September 24, 2000 6:05:41 PM
Stoffel
Stoffel
You can assign specific values to enums, they don''t need to be in order:

enum DikType
{
DIK_1 = 0x02,
DIK_9 = 0x0A,
DIK_0 = 0x0B
};


I like enums because they show up in the debug watch window as "DIK_1" instead of 9 or 0x09.

The previous posters are correct: whether you assign them as hex, decimal, or octal, they''re still represented the same in the code.

I personally hate #defines with a passion, since they have no scope (i.e. they''re all global), so it makes re-using code quite difficult. I just finished a project where I had to create a library under a separate project that didn''t know abut windows at all beause the code I needed to integrate had #defines that collided with those in window.h. Major pain.

If you don''t want to use an enum, this is (IMHO) better than a #define:
static const int DIK_1; // in your header file
const int DIK_1 = 0x02; // in your C file

This will let you use scope, and you can #include that header file in a namespace later if you happen to collide somewhere down the line.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.