MAKE VERSION macro? How to do?
Hello i want to do preprocessor MACRO like MAKEVERSION(a,b,c,d)
and after i'll call it like this MAKEVERSION(0,2,3,56) i'll get a DWORD = 0.2.3.56
Is it possible?
Thanks
#define DW(x) ((unsigned long)(x))#define MAKEVERSION(a,b,c,d) DW((DW(a) << 24) | (DW(b) << 16) | (DW(c) << 8) | (DW(d))#undef DW
Quote:Original post by MiniosNot exactly, since a DWORD can only store one integer.
Hello i want to do preprocessor MACRO like MAKEVERSION(a,b,c,d)
and after i'll call it like this MAKEVERSION(0,2,3,56) i'll get a DWORD = 0.2.3.56
Is it possible?
Thanks
You can however, pack the values in so you'd get DWORD = 0x00020338, which represents what you want with the following:
#define MAKEVERSION(a, b, c, d) (DWORD)( (((a)&0xff)<<24) | (((a)&0xff)<<16) | (((a)&0xff)<<8) | ((a)&0xff))
EDIT: Screwed up the masks
#define MAKEVERSION(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))
will give you
DWORD version = 0x0a0b0c0d
The only limit on this is that each number must be between 0 and 255.
To print out the version number as a string,
printf("Version: %d.%d.%d.%d", (version & 0xff000000) >> 24, (version & 0x00ff0000) >> 16, (version & 0x0000ff00) >> 8, version & 0x000000ff);
will give you
DWORD version = 0x0a0b0c0d
The only limit on this is that each number must be between 0 and 255.
To print out the version number as a string,
printf("Version: %d.%d.%d.%d", (version & 0xff000000) >> 24, (version & 0x00ff0000) >> 16, (version & 0x0000ff00) >> 8, version & 0x000000ff);
Big Thanks!! :D
And how i can print it in this way: 0.2.3.56?
I mean when i;m doing printf i'm getting a long number like 106842335..
Thanks again.
Sorry didnt noticed Sly's comment
Thanks ppl :)
And how i can print it in this way: 0.2.3.56?
I mean when i;m doing printf i'm getting a long number like 106842335..
Thanks again.
Sorry didnt noticed Sly's comment
Thanks ppl :)
typedef unsigned long dw;dw v = MAKEVERSION(1,2,3,4);dw a=(v >> 24) & 0xff;dw b=(v >> 16) & 0xff;dw c=(v >> 8) & 0xff;dw d=v & 0xff;printf("%u.%u.%u.%u.", a, b, c, d);
Original post by Evil Steve#define MAKEVERSION(a, b, c, d) (DWORD)( (((a)&0xff)<<24) | (((b)&0xff)<<16) | (((c)&0xff)<<8) | ((d)&0xff))
Fixed it for you.
Since you mention DWORDS, it's likely you're talking about C++.
You can also change the types of individual A,B,C,D to be different from 8-bit int, perhaps 16-bit for D, to allow more builds.
But the best thing is, there is no overhead, there is no storage, and no #defines.
You can optionally make all methods static.
Excercises left for the reader:
- overload << operator
- overload comparison operators
etc...
typedef unsigned char ver_t;template < ver_t A, ver_t B, ver_t C, ver_t D >struct version{ DWORD operator()() const { return (A << 24) | (B<<16) | (C<<8) | (D); } ver_t Version() const { return A; } ver_t Major() const { return B; } ver_t Minor() const { return C; } ver_t Build() const { return D; } void print() const { std::cout << static_cast<int>(A) << '.' << static_cast<int>(B) << '.' << static_cast<int>(C) << '.' << static_cast<int>(D); }};typedef version<0,2,3,56> BuildVersion;typedef version<1,0,0,22> ProtocolVersion;....
You can also change the types of individual A,B,C,D to be different from 8-bit int, perhaps 16-bit for D, to allow more builds.
But the best thing is, there is no overhead, there is no storage, and no #defines.
You can optionally make all methods static.
Excercises left for the reader:
- overload << operator
- overload comparison operators
etc...
Quote:Original post by KonfusiusQuote:Original post by Evil Steve
*** Source Snippet Removed ***
Fixed it for you.
Whoops, damn copy & paste errors...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement