how do I implement a counter from 0-C hex

Pages: 12
Feb 3, 2011 at 10:30pm
and how would I increment it and make it skip D-F (hex) every time it got there.
Feb 3, 2011 at 10:34pm
This should do what you mean.
1
2
3
4
5
6
7
for (int i = 0x0C; i < 0xFF; ++i)
{
  if  (i == 0xDF)
    continue;

  // Test
}
Feb 3, 2011 at 11:00pm
Thanks
I meant make it skip D,E, and F and go to the next 1
Thanks again for the help.
Feb 3, 2011 at 11:21pm
Well if you only want to skip the last digit meaning d0-dc is okay and dd-df is not then use %;

if (i % 0x10 == 0xd) // Repeat for 0xe and 0xf
Feb 3, 2011 at 11:59pm
What I need is a counter that counts in hex starting from 0 to C while skipping D-F all the while getting bigger untill another counter hits it's preset limit. So I could have DC4A...etc.
I want there to be a D in front of the number example D0 , D1 and after it reaches C then the output would be DD0
Thankyou
Last edited on Feb 4, 2011 at 12:35am
Feb 4, 2011 at 12:31am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Proof of concept.
#include <iostream>

bool is0xDEF(int val) // Simple bool function
{
  int remainder = 0x0;
  while (val != 0x0) // Repeat these actions until value is 0.
  {
    remainder = val % 0x10; // Divide by 0x10 (16)
    // This gives us the last digit of the hex number.
    // So 0x4DA % 0x10 returns A.
    if (remainder > 0xC) return true; // if remainder > 0xC (12)
    // If this is true, the last digit is D, E, or F
    // Which we don't want. If this does not pass, we go to this step:
    val = val / 0x10;
    // This removes the last digit.
    // So 0x4DA is now 0x4D and repeats the check above.
  }

  // We found no D's, E's, or F's in the loop. We can return false.
  return false;
}

using std::cout; using std::endl;
int main()
{
  cout << std::boolalpha; // boolalpha just says true/false instead of 1/0
  cout << is0xDEF(0xCD4) << '\n';
  cout << is0xDEF(0xC4A) << '\n';
  cout << is0xDEF(0xD33) << '\n';
  cout << is0xDEF(0x00D) << '\n';

  for (int i = 0x0; i < 0xFF; ++i) //Here's a loop for proof.
  {
    // If we see 0x## where ## is a d, e, or f, it will skip the printing of the number.
    if ( is0xDEF(i) ) continue;
    cout << std::hex << i << " : " << std::dec << i << '\n';
    // Shows hex and decimal equivalent of i.
  }
}


If you want to only skip the 0xDD# where # is D, E, or F then you only need to % the number and check if its > 0xC
Feb 4, 2011 at 12:44am
Thankyou so much for the code and the explanation.
Last edited on Feb 4, 2011 at 12:46am
Feb 4, 2011 at 1:38am
I am still confused about how to increment by 1 and start out with D0 and everytime it gets to D it puts another D on the total example= D0 ,D1, ...DD0,DD1 etc...





Feb 4, 2011 at 1:46am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// WATCH AS I BLOW YOUR MIND
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
  for (int i = 0xD0; i < 0xDDD5; ++i) // The loop limits for testing
  {
    if ( (i % 0x10) > 0xC ) // Remember that % gives me the last digit.
      i *= 0x10; // If the last digit is D, we multiply by 0x10 (16) 
      // This makes the last digit 0 again. Your mind should be blown here.

    cout << std::hex << i << " : " << std::dec << i << endl; // Proof.
  }

  return 0;
}
Feb 4, 2011 at 1:48am
Thankyou for being so patient with me and for helping me, I believe I understand now.
Feb 4, 2011 at 1:48am
I think wolfgang is confused as to what you want.

You want it to count 0 1 2 ... 9 A B C 10 11 12 ... 19 1A 1B 1C 20 21 ... right?
Use the modulo operation to help.

1
2
3
4
5
6
for (unsigned n = 0; n < 0x100; n++)
  {
  if ((n % 0x10) > 0xC) continue;  // this is the "skip D-F" part
  cout << hex << n << "\t";
  }
cout << endl;

Hope this helps.
Feb 4, 2011 at 1:54am
One thing that confused me was that he edited his post before after my second code post, so he added the D before the C4A. So then the code I posted which removed all D's was invalid.

Then he said he wanted D in front. I don't mind running in what seems like circles but the code Duoas posted will post numbers like 0xDC then jump to 0x100. If you increase the range to 0x1000 it will run to 0xDDC then skip to 0x1000. Does that make sense?
Feb 4, 2011 at 2:12am
I'm sorry that I confused everybody. I do want D I want it at the start and everytime the counter reaches D. It made perfect sense when I first thought it up, but I did not write it down and now I am confused as well.
Okay D0 to DD (not skipping D's as I first thought) all the way to 0xffffffff
You want it to count 0 1 2 ... 9 A B C 10 11 12 ... 19 1A 1B 1C 20 21 ... right?
No I don't want any numbers skipped just reencoded to the new scheme.
Last edited on Feb 4, 2011 at 2:43am
Feb 4, 2011 at 2:45am
Change 0xC to 0xD. Done.
Feb 4, 2011 at 3:06am
thanks
Feb 4, 2011 at 12:15pm
This is going to sound strange(as if anything I have already said isn't) but how do I make the program stop when it encounters an E in either digit -an error? I know that it will use break, but what if the digit is in the ones place instead of the tens place?
Thanks
Last edited on Feb 4, 2011 at 12:24pm
Feb 4, 2011 at 12:28pm
Perhaps you'll do better if you just tell us what you are trying to do, and we'll be able to give you better help.
Feb 4, 2011 at 1:50pm
Explain the series of numbers you're trying to count and what you want to do if you encounter them.
Feb 4, 2011 at 2:06pm
I am trying to count from 0x00 to 0xFF and beyond but avoiding the numerals E and F(these will be for the operation to break i.e. errors)
Thanks for being so patient with me.
Feb 4, 2011 at 2:19pm
How you avoiding them? Do you want to skip E0 E1 E2 E3 E4... till EF and F0 F1 F2 F3 F4... until FF or do you only want to skip E as the ones digit so E0-ED is fine and so is F0-FD but EE EF FE and FF are not. Explain the counting.
Last edited on Feb 4, 2011 at 2:20pm
Pages: 12