Jerry_k
Posts: 31
Joined: Wed Oct 10, 2012 7:20 pm

little problem with if statement

Sat Jun 22, 2013 10:17 pm

hi, I'm new in C and I need help..

I don't know how to write "if statement"..

On picture attachment is my card_UID:
print_hex(nt.nti.nai.abtUid, 4);

I want write something like this - if card id = 0a 1f 33 c4 do this... but don't know how to write "0a 1f 33 c4" to if..
this is not working..
if ((nt.nti.nai.abtUid, 4) == (0x0a, 0x1f, 0x33, 0xc4)) {

this is not working too:
if ((nt.nti.nai.abtUid, 4) == ("0a 1f 33 c4")) {

Thank you very much!!
Attachments
UID.JPG
UID.JPG (19.64 KiB) Viewed 1165 times

User avatar
Redrobes
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: little problem with if statement

Sat Jun 22, 2013 10:56 pm

it depends on what "type" your nt.nti.nai.abtUid is. It would tell you in the nfc.h include file tho. Assuming its an "unsigned int" then its probably:

if( nt.nti.nai.abtUid == 0x0a1f33c4 )
{
do something....
}

You may find that it could be

if( nt.nti.nai.abtUid == 0xc4331f0a )
{
do something....
}

because of the "endian". You could check it before you write the line by using something like:

printf( "The value is 0x%08x\n", nt.nti.nai.abtUid );

and then see what it prints when you run it.

Jerry_k
Posts: 31
Joined: Wed Oct 10, 2012 7:20 pm

Re: little problem with if statement

Sun Jun 23, 2013 7:22 am

Great, thank you very much,
but UID is without change and "the value" is changed every one time.. Do you know what exactly I must write to "If statement"? I'm little confused when it is changed every time...
Attachments
UID.JPG
UID.JPG (26.78 KiB) Viewed 1126 times

Jerry_k
Posts: 31
Joined: Wed Oct 10, 2012 7:20 pm

Re: little problem with if statement

Sun Jun 23, 2013 11:44 am

definitely solved by:
if (nt.nti.nai.abtUid[0] == 0x00000021) {


Thank you!

PS: next will add something like:
nt.nti.nai.abtUid[0] == 0x00000021 && nt.nti.nai.abtUid[1] == 0x000000d5 && nt.nti.nai.abtUid[2] == 0x0000006f && nt.nti.nai.abtUid[3] == 0x000000e8

if you know how to short it, please post, but it is not necessary :)

scrapheap
Posts: 20
Joined: Wed Feb 13, 2013 5:13 pm

Re: little problem with if statement

Tue Jun 25, 2013 12:43 pm

nt.nti.nai.abtUid is an array and as such is a pointer unless you are using an offset (i.e. indexing into the array) or dereference it (combine the dereference with a cast to get the type of value you are looking for).

Here's an example of dereferencing a cast of a similarly defined array.

Code: Select all

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

uint8_t sample[4]={0x01,0x02,0x03,0x04};

int main(int argc, char ** argv){
  printf("Sample: 0x%0X\n", sample);
  printf("Sample: 0x%0X\n", *(uint32_t *)sample);

  return EXIT_SUCCESS;
}
Personally, though, I would stick with the current method you are using as the code is more readable, more portable (read up on Endianness to see why) and it won't hit the performance of the code in real terms.

Return to “C/C++”