UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

#1 2010-10-22 08:32 PM

RM
Member
Registered: 2010-10-22
Posts: 9

How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Hi

I have a function that receives a DWORD value, and it must be a address code that start at 0x00000001 until 0xFFFFFFFF. How can I create a DWORD variable and increment it only with hex-values in a way it will start with 0x00000001 and finish with 0xFFFFFFFF?

For example, the output at each loop should be:

0x00000001
0x00000002
0x00000003
0x00000004
0x00000005
0x00000006
0x00000007
0x00000008
0x00000009
0x0000000A
0x0000000B
0x0000000C
0x0000000D
0x0000000E
0x0000000F
0x00000010
0x00000011
0x00000012
etc...

Thanks

Offline

#2 2010-10-22 09:14 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

I must be missing something about what you're asking...  Because, it sounds to me like you just want to do essentially printf("0x%08x",i++); in a loop...

Offline

#3 2010-10-23 04:51 AM

RM
Member
Registered: 2010-10-22
Posts: 9

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Rob

You are right, but I need this to be stored in a DWORD variable for each iteration of the loop. Why? Because I have to pass it to a API under Windows that typedef is DWORD.

I believe I could do your loop with a sprintf() and "0x%08x" or equivalent, but how to store it properly in a DWORD? If it's not a DWORD the API that I'm calling will fail...

This DWORD for me is kind of new, it appears to be a MS world typedef (http://www.gamedev.net/community/forums … c_id=80721)

Thanks

Offline

#4 2010-10-23 01:24 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

DWORD stands for double word, meaning a 32 bits integer.
A word was used for a short integer of 16 bits. So just treat
it as a regular int or unsigned int and you should be fine.

It's not some magic HEX string value based variable or anything
crazy like that, it's a regular int with a stupid typedef.

Edit: It seems the link you posted explains it quite well...

Last edited by i3839 (2010-10-23 01:26 PM)

Offline

#5 2010-10-23 05:37 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Yeah, what i3839 said...  So, all you need is to define a variable of type "DWORD" (or you can use "unsigned int" if you prefer; they're just different names for the same thing), and then just increment that normally as you would any int...  There's no magic involved...  And, there's nothing special needed just because the function wants to treat the value as hex instead of decimal or whatever...  The raw int value remains the same regardless...  (After all, in reality, everything is stored in binary, anyway...)

Offline

#6 2010-11-01 12:58 PM

RM
Member
Registered: 2010-10-22
Posts: 9

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Hi

Thanks for your help.

I did a loop with a unsigned int and it works fine.

#include <stdio.h>

int main(){

unsigned int loop;

for ( loop = 0x00000001; loop <= 0xFFFFFFFF ; loop++){

        printf ("Value is 0x%08x\r\n", loop);
        // LSPFunc(loop, SZ_READ, RNOW);

}

return(0);
}

However, when I uncomment LSPFunc it never appear to send the right values. I mean, on the printf it's correctly printed because we are using the right typo (0x%08x). There is a way to cast it as hex to pass it as first parameter to function LSPFunc()?

Thank you

Offline

#7 2010-11-01 06:35 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

What's the prototype of that LSPFunc()?

Offline

#8 2010-11-01 08:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

That's an infinite loop you've got there, too, since you do <=...  Once you increment 0xffffffff, you'll get 0, which is still less than, so you'll keep going forever...

But, again, it makes no sense to talk of "casting to hex", since hex is merely an interpretation of the raw binary value, which is what you're actually passing...  The function will interpret it as hex if that's what it wants...

So, what makes you think it's NOT getting the correct values?  What values do you think it IS getting instead?  Is this LSPFunc() something of yours, or something you have the source to at least?  If so, can we see it?

Also, why not simply declare the type of your local loop variable as "DWORD", if that's what type the function prototype uses?  You can still increment it just like that...

Offline

#9 2010-11-01 09:01 PM

RM
Member
Registered: 2010-10-22
Posts: 9

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Hi i3839

The prototype of LSPFunc is:

BOOL LSPFunc(
   __in         DWORD Code,
  __in          DWORD Operation,
  __in_opt    DWORD Option
);

Offline

#10 2010-11-01 09:04 PM

RM
Member
Registered: 2010-10-22
Posts: 9

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Hi RobSeace,

Make sense. So, how to do this loop if I can't do comparison with 0xFFFFFFFF?

Humm, how I can printf values of a DWORD in binary? If possible with a space at each 8 bits. There is a way to do it?

What make me believe it's wrong is that I always get return error that says "wrong code".

It's a 3rd party library, I don't have the source code, just the prototype, and a brief of return values.

Thanks, I did the loop with DWORD.

Offline

#11 2010-11-01 10:28 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

RM wrote:

So, how to do this loop if I can't do comparison with 0xFFFFFFFF?

Well, since you don't seem to need to try 0, you could just change the loop condition to "loop != 0" (or more simply, just "loop")...  Or, if you didn't want to rely on the effects of integer overflow, you could get rid of the loop condition entirely, and do the test inside at the bottom of the loop: "if (loop == 0xffffffff) break;"...  Or, in the same style, convert it to a do{}while() loop, where the test is naturally on the bottom...

Humm, how I can printf values of a DWORD in binary? If possible with a space at each 8 bits. There is a way to do it?

There's no C standard printf() format for outputing binary, unfortunately...  You can do it fairly easily manually, though:

void print_binary (uint val)
{
    int i;
    for (i = 32; i > 0; i--)
        printf ("%s%s", (i % 8) ? "" : " ", (val & (1 << (i-1))) ? "1" : "0");
    printf ("\n");
}

But, why bother?  Hex is much easier to read, and is trivial to convert in one's head directly to binary...  (Well, trivial for ME, at least...  I would've assumed it would be so for any programmer, as well, though...)

What make me believe it's wrong is that I always get return error that says "wrong code".

Well, maybe it doesn't like those values you're giving it?  Is there some reason to believe it should support every single value between 1 and 0xffffffff?  I'm guessing you're simply calling it wrong...

Offline

  • Index
  • » C
  • » How to loop a DWORD from 0x00000001 until 0xFFFFFFFF?

Board footer

Powered by FluxBB