Muhammed Yıldız Muhammed Yıldız - 2 months ago 11
C++ Question

How to read elements of an array?

I am taking data from serial port using these commands:

char buf [256];
char a [256];
memset (&buf, '\0', sizeof buf);

/* *** READ *** */
if (n < 0)
{
std::cout << "Error reading: " << strerror(errno) << std::endl;
}

/* Print what I read... */
std::cout << "" << buf;
int sa=0;
printf("Gelen Data : \n");

for(sa=0;sa<15;sa++){
printf("::%0.2X",buf[sa]);
}


and this is my data:

::02::03::98::00::07::20::16::09::29::16::19::45::C0::00::00::00


I can see data coming using these commands:

for(sa=0;sa<15;sa++){
printf("::%0.2X",buf[sa]);
}


but I don't use elements of the
buf[]
array in any equation; for example, I want to use
buf[2]
in an equation like below:

if(buf[2]==02){
cout<<"Equality"<<endl;
}


Can anybody help me?

dd2 dd2
Answer

You might want to compare them in hexadecimal format.

if( buf[2]==0x02){
      cout<<"Equality"<<endl;
}