CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Compare hex with string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

Compare hex with string
PostPosted: Fri Mar 25, 2016 3:53 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Fri Mar 25, 2016 5:47 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 25, 2016 7:11 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Fri Mar 25, 2016 8:45 pm     Reply with quote

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: 19563

View user's profile Send private message

PostPosted: Sat Mar 26, 2016 1:46 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group