Comparing uint8_t data with string

前端 未结 1 771
别跟我提以往
别跟我提以往 2021-01-25 05:58

This may sounds little odd or question may be a trivial one, but for most of my life I was programming in PHP (yeah, I know how it sounds). So when I switched to C++ there are t

相关标签:
1条回答
  • 2021-01-25 06:49

    Strings in C and C++ are null-terminated. RIFF and WAVE aren't technically C-style strings because there is no null terminator, so you can't just use a straightforward C/C++-style string compare like strcmp. There are however several ways you could compare them against the strings you want:

    • if (header.RIFF[0] == 'R' && header.RIFF[1] == 'I' && header.RIFF[2] == 'F' && header.RIFF[3] == 'F') { // .. }
    • if (strncmp((const char*)header.RIFF, "RIFF", 4) == 0) { // .. }
    • if (memcmp(header.RIFF, "RIFF", 4) == 0) { // .. }

    I would personally use either strncmp or memcmp. They end up doing the same thing, but semantically strncmp is a string compare function which maybe makes the code clearer.

    For strncmp see here. For memcmp see here.

    0 讨论(0)
提交回复
热议问题