-1

I have a function that calculate a 32 bit CRC,

I have an internal variable to a class that hold the CRC.

the function that validate CRC consistency return a boolean result of the class CRC with what the class is calculating,

the member function inside the class does the next,

save the CRC into a temp variable, calculate CRC then compare the two values.

bool X::CRCisMatching()
{
   unsigned long tmpCRC = ClassCRC;
   ClassCRC = GetNewCalculatedCRC();
   printf("comparing %08x ~ with ~ %08x!\n", tmpCRC, ClassCRC);
   return tmpCRC == ClassCRC;
}

the issue is that I get this output,

comparing AB44CD2A33 ~ with ~ AB44CD2A33

why I am getting 10 values instead of 8 as intended with %08x

did anyone face any similar problem before??

btw, I am using a 32 bit machine

aah134
  • 860
  • 12
  • 25

2 Answers2

0

Because %08x means a minimum length of 8, not maximum. And whether your machine is 32-bit or 64-bit does not necessarily control the size of a long. I suggest you print out sizeof(unsigned long) to confirm its size.

Normally (though not guaranteed), a 32-bit systems will have a 32-bit unsigned int. C provides specific-width types like uint32_t and this is also available in C++11 via the cstdint header. (assuming there's an underlying type that provides it).

My advice would be to use that type if your compiler is modern enough, otherwise, assuming that you discover sizeof(unsigned int) == 4 (and your bytes are eight bits), that's the type you should probably use.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • oh so if I am getting a bigger number, it mean I am using a bad data type?? I will print out the size of long to find out tomorrow morning – aah134 Aug 01 '13 at 02:24
  • @abdul, no, not necessarily a _bad_ type. You could always do your calculations modulo 2^32 but you're probably better off using one that automagically does that for you, such as `unsigned int` (though you should check the size of that as well). – paxdiablo Aug 01 '13 at 02:26
  • I will probably adjust the data type to UINT32 which is already provided by the vendor, to be safe, and find out what happens next. – aah134 Aug 01 '13 at 02:31
  • 1
    C++ does have `uint32_t` et al under the `cstdint` header. – Rapptz Aug 01 '13 at 02:36
  • @Rapptz, yes, just confirmed that, c++11 added those types. Adjusted answer to suit. – paxdiablo Aug 01 '13 at 03:33
0

sizeof(int) is not as expected and printf() (or the post) is amiss.

printf("comparing %08x ~ with ~ %08x!\n", tmpCRC, ClassCRC);

OP reports this printed "comparing AB44CD2A33 ~ with ~ AB44CD2A33". This implies that the format specifier "%08x", which is looking for an int, found an int with a value that needs more than 4 bytes to represent. This hints that an int is 8 bytes. The reported 32 bit machine suggests, but does not specify an int width. Further, the 8 in %08x only specifies the minimum print width.

OP passed a unsigned long and the print specifier expected an unsigned. I suspect this did not affect the issue supposing these 2 types are the same on OP's machine. Good code would have used a printf specifier of %08lx to matched unsigned long.

OP reported output of "AB44CD2A33". With a %x format specifier, an output of "ab44cd2a33" would be expected. Although this may simply be a transcription error, it raises concerns that the post is errant in other places or that the compiler is not compliant.

Should the OP want to use a definite 32-bit value for the 32-bit CRC, recommend using type uint32_t and PRIX32 for printing.

#include <inttypes.h>
uint32_t tmpCRC = ClassCRC;
printf("0x%" PRIX32 "\n", tmpCRC);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256