How to check for multiple characters in a char array?

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hiya.

I have a char array where I want to check bytes 5-16 against a given string, for the purpose of identifying a product I'm communicating with.

If the amount of characters were smaller, I'd just use an "if byte[n] == 0x??" check. But with 11 bytes, this seems a bit brutal. Is there a more elegant way to do this?

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about:

if (strncmp(&to_test[5], "is it this", 10) == 0) {

where 10 is the length of "it it this"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is there a more elegant way to do this?


I don't know if it is less brutal or more elegant, but the standard library should have some functions in string.h to help you out. Now, your character array then could not contain nulls, and must be null terminated.

Essentially the "savings" is having the pre-written routine do the byte-by-byte comparison for you.

For any spot in the string, start with strstr(). If you have more position information then strncmp()or memcmp().

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff, Cliff.. What would you say if someone you're bossing around at work all day produced that code? :wink:

I'd expect at least this:

#define THE_NAME "is it this"
  .
  .
  .
if (strncmp(&to_test[5], THE_NAME, strlen(THE_NAME)) == 0) { 

And then awew get into the discussion about tossing that string (those strings?) into PROGMEM..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Johan,

Yup I knew that as I typed it but I was trying to keep it as simple as possible to show the "raw" use of strncmp and not overload/complicate it with the obvious use for strlen() (at least I hoped it WOULD be obvious!)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

at least I hoped it WOULD be obvious!

I was only teasing you. And now, looking at my own suggestion I'm not sure that that is any good at all. It's late at work, and my brain just brown-out'ed...

Strncmp will stop at any null character (or maybe not?) in any of the strings, and "is it this" will be null-terminated regardless of how we play around with it. And it is 10 characters, not 12 as is the case with positions 5-16. Is there a null terminator anywhere in those positions? Why is OP saying that pos 5-16 is 11 characters rather than 12? Why did I decide to spend my life in the s/w bussiness, and not as an anthropologist on a coral island?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
Cliff, Cliff.. What would you say if someone you're bossing around at work all day produced that code? :wink:

I'd expect at least this:

#define THE_NAME "is it this"
  .
  .
  .
if (strncmp(&to_test[5], THE_NAME, strlen(THE_NAME)) == 0) { 

And then awew get into the discussion about tossing that string (those strings?) into PROGMEM..


if(strcmp(&to_test[5], THE_NAME))==0) {

is better.
It does the same job whether to_test has good data or not.
THE_NAME provides the bound.

strcmp_P might be better.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In BASIC:

For I = 5 to 16
    If Chr(Array(I)) <> Mid(CompareString, I - 4, 1) Then
        ComparePassed = False
    End If
Next I

If ComparePassed = True
    ...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

In BASIC

Fascinating, and rare I suppose. A "Unix" that talks BASIC... :shock:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Haha well I can manage Python (which is very close to BASIC anyways), tcl, and moderate C. Just never took the time to properly learn C on embedded platforms... ;)

I'll edit this post not to clutter this thread more:

Python is pretty close to BASIC in syntax... Closer to BASIC than to C anyways. ;)

age = input("Enter your age (in human years): ")
print

if age < 0:
	print "Negative age?!?  I don't think so."
elif age < 3 or age > 110:
	print "Frankly, I don't believe you."
else:
	print "That's", age*7, "in dog years."

Same thing in BASIC:

Input "Enter your age (in human years): ", Age
Print

If Age < 0 Then
	print "Negative age?!?  I don't think so."
Elseif Age < 3 or Age > 110 Then
	print "Frankly, I don't believe you."
Else
	print "That's", age*7, "in dog years."
End If
Last Edited: Thu. Oct 23, 2008 - 10:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Python (which is very close to BASIC anyways)

You need to get off that bottle.. 8)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the help. I landed on using a combination of the strncmpf() and strlenf() functions, since I had some issued with SRAM vs FLASH.

I've never been too good on memory handling.. :oops: :D