0

I have a C program to calculate crc of a message that is represented in hex numbers:

unsigned int CRC_Check(unsigned char *ucCRC_Buf, unsigned char ucBufLength)
{
  //calculate crc
  return calculated_crc;
}

int main(void)
{
  unsigned int crc=CRC_Check("\x00\x01\x90\x16\x55\x00\x2c\x10\x01\x01\x01\x0e\x10\x10\x01\x01\x01\x3a\x11",19);
  printf("%x\n",crc);
  return 0;
}

Now I need to pass the message to the program as argument:

./crc 0001901655002c100101010e10100101013a11

I know that main declaration should look like this:

int main(int argc, char *argv[])

and that argv[1] is my message. How do I achieve so the program sees it as individual bytes so I can do something like this:

unsigned int crc=CRC_Check(argv1_as_hex_bytes,19);

Thanks!

muliku
  • 416
  • 3
  • 17
  • I don't see how my question is duplicate. Ok, maybe the first part about converting string to hex bytes. But I still need help with how to pass it to the function CRC_Check(). When I pass the string it calculates wrong crc – muliku Sep 04 '17 at 13:18
  • In that case, your question will be closed because you didn't provide an [MCVE](https://stackoverflow.com/help/mcve)... I suggest carefully thinking about your questions, whether you're asking for what you need to ask for to get the answer and whether you're giving us the information we need to be given to give the answer... Move on, ask a new question using this advice as guidance. Spend at least half an hour thinking about (and perhaps even researching) what to put in your question and how to organise it. – autistic Sep 04 '17 at 13:31
  • Additional information is also required. There are many different flavours of CRC, and FWIW many of them are 32-bit and so belong in an `unsigned long` or `uint_least32_t`; something that's guaranteed to be at least 32-bits (unlike `unsigned int`). If you use one of the 16-bit CRCs and you know all of this, so be it, but I just thought I'd let you know because integer choice is one of the biggest mistakes I've noticed in the realm of C. – autistic Sep 04 '17 at 13:39

0 Answers0