View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
Compare hex with string |
Posted: Fri Mar 25, 2016 3:53 pm |
|
|
Hi, I have one hex "0x01" and I want compare with string "1", please someone know how I do it?
Code: | key[2];
if(memcmp(&BufferFAT[k], key , sizeof(key)) == 0 )
{
fprintf(uart1,"ok"); break;
} |
for example: if in any moment key[2]={0x01} and bufferFAT[0]={1}
how can i do the comparison?
thanks |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Mar 25, 2016 5:47 pm |
|
|
in a string "1" is ascii 48
and 0x1, 0b1 and 1 are still 1
btw what you posted does no compile either.
of those characters - what were you expecting
ASC 1 or ASC 48 ? |
|
|
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
|
Posted: Fri Mar 25, 2016 7:11 pm |
|
|
asmboy wrote: | in a string "1" is ascii 48
and 0x1, 0b1 and 1 are still 1
btw what you posted does no compile either.
of those characters - what were you expecting
ASC 1 or ASC 48 ? |
Thank you for answer.
I want compare "01" hex with "01" string.
02 with "02" string
FF with "FF" string... |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Mar 25, 2016 8:45 pm |
|
|
that's not how memcmp() works.
memcmp is a byte for byte comparison
to find a hexbyte of 0x01 you need to be looking for that same byte
you are being way to vague and
more importantly - yours is a general 'C question
and what does this have to do with CCS?
please check out
http://www.ccsinfo.com/forum/viewtopic.php?t=26245
with special attention to number 4 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Mar 26, 2016 1:46 am |
|
|
Basic C 101.....
Generally though, work out yourself the best way to do this. Either print the hex to a string, which then means you can do a direct string compare, or convert the string to it's numeric equivalent, and compare this. Look at the strtol conversion, and sprintf. |
|
|
|