Home > Enterprise >  increment hex value by a certain value in C ?
increment hex value by a certain value in C ?

Time:03-02

I have a hexadecimal variable that I want to increase its inc value by x20 in every loop. For example for 10 rounds, inc value increase by 0x20 and add to the pam in each loop.

but now i'm getting 1060,1061,1063,1066,106a,106f,1075 etc...

int main() {
  int inc = 0x20;
  int pam = 0x1040;

  for ( int i = 0; i < 10; i    ) {
      inc = inc  ;
      cout << pam inc << endl;
      }
  return 0;
}

What I want to get is 1040, 1060, 1080, 10A0, etc. Example;

Output:
pam   inc
pam   inc   inc
pam   inc   inc   inc
etc...

CodePudding user response:

Here's a method to increment numbers using a fixed increment:

const int inc = 0x20;
//...
for (int i = 0; i < 10;   i)
{
  pam  = inc;
  std::cout << "pam: " << pam << "\n";
}

If you can't modify pam then modify the increment:

int inc = 0x20;
for (int i = 0; i < 10;   i)
{
    inc  = 0x20;
    std::cout << (pam   inc) << endl;
}

Applying some math:

for (int i = 0; i < 10;   i)
{
    std::cout << ((inc * i)   pam) << std::endl;
}
  •  Tags:  
  • c
  • Related