0

Goodday

Im currently checking for a true condition in a incoming char buffer as follow. below im looking for 197403290000000000123456789ABC0000 in a BLE string.

uint8_t buf[BLE_MAX_ADV_DATA_LEN];

 if ((buf[13]==0x19)&&(buf[14]==0x74)&&(buf[15]==0x03)&&(buf[16]==0x29)&&(buf[17]==0x00)&&(buf[18]==0x00)&&
(buf[19]==0x00)&&(buf[20]==0x00)&&(buf[21]==0x00)&&(buf[22]==0x00)&&(buf[23]==0x12)&&(buf[24]==0x34)&&
(buf[25]==0x56)&&(buf[26]==0x78)&&(buf[27]==0x9A)&&(buf[28]==0xBC)&&(buf[29]==0x00)&&(buf[30]==0x00)){   

  then do stuff
} 

It does work but is there a better way to compare a string segment and compare it to a known segment.

  • I would encapsulate the compares into a function, so that your if statement isn't that bloated, but I don't know better way to get rid of all the comparisions. You could also translate the hex-values to the corresponding ascii char and then use a string compare function, but then information get lost – RoQuOTriX Dec 10 '19 at 07:58
  • Please don't use [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming))! – Some programmer dude Dec 10 '19 at 08:00
  • And you could solve your problem simply by creating a second buffer containing only the data you want to compare with, and then use [`std::memcmp`](https://en.cppreference.com/w/cpp/string/byte/memcmp). – Some programmer dude Dec 10 '19 at 08:00

1 Answers1

0

You can use memcmp and compare your buffer against a hard-coded array. I've also added a handy function that can return the number of items in a statically array (If you build on windows you can just use _countof ). Starting with C++ 17 you can use std::size.

#include <cstring>  // has the declaration of memcmp

template<typename T, size_t SizeOfArray>
constexpr size_t countof(T (&array)[SizeOfArray]) { return SizeOfArray; }

The actual code:


    uint8_t expectedValues[] = {0x19, 0x74, 0x03, 0x29, ...};
    uint8_t buf[BLE_MAX_ADV_DATA_LEN];

    if (0 == memcmp(buf, expectedValues, countof(expectedValues)))
    {
            // the buffer contains the expected values
    }
Claudiu Guiman
  • 837
  • 5
  • 18
  • This is not a valid answer, since this is MS-only and the question is not. In standard C++ we have std::count and std::count_if. – nada Dec 10 '19 at 09:04
  • How is it non standard? I've provided the declaration of countof. – Claudiu Guiman Dec 10 '19 at 10:12
  • Sorry, I meant std::size. Why not use that? The name _countof_ is misleading (it mislead me), because we have std::count in the C++ library, which does something else. – nada Dec 10 '19 at 10:15
  • @nada I wasn't aware of it, every day you learn something new. Thanks! :). I've updated the answer. – Claudiu Guiman Dec 10 '19 at 10:19
  • There is [this question](https://stackoverflow.com/questions/4415530/equivalents-to-msvcs-countof-in-other-compilers) on cross-platform pre C++17 countof alternatives. OT: Yes, C++ is getting more beautiful with each new standard (imo of course) and I hope it stays like that. C++17 even has std::filesystem ;) wow – nada Dec 10 '19 at 10:25