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

fgets() with hex problem

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



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

fgets() with hex problem
PostPosted: Sun Nov 04, 2007 7:47 pm     Reply with quote

I am trying to receive values from a serial port of a pc and the format is as follows:

0x450x110x0D

The decimal format is 691713

I am using fgets(data,COM_A)! Should I use fgetc instead?

How can I compare this in an if strcmp(data, "691713")?

Do I need to convert this before the compare?

This serial stuff is killing me!

Thanks in advance,

Scott
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 3:01 am     Reply with quote

Yes you need to convert the values to a 24 bit number before doing the compare OR you can do

strcmp(data, "0x450x110x0d")

to convert would have been simple IF the compiler included the sscanf function. Not sure what compiler you are using, you can check if you want.

Otherwise you will have to do it manually.
Repeat 3 times the following
Read 4 chars, skip the first 2 and convert the hex value to decimal

If you need more help in doing that then post!
Scottl



Joined: 21 Feb 2005
Posts: 25

View user's profile Send private message

PostPosted: Thu Nov 08, 2007 9:12 pm     Reply with quote

Hi Wayne,

Been out traveling this week! The customer has change the message to ASCII "EDC1" which in hex is 0x450x11. When I get the string can I compare as is or still need converting to DEC?

If I still need conversion can you give me any pointers?

Thanks for your help,

Scott
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Nov 12, 2007 3:39 am     Reply with quote

Is the message "EDC1" or "0x450x11"

The characters coming in are what you have got to compare NOT what they represent unless you convert them.

so either strcmp(data, "EDC1") or strcmp(data, "0x450x11") would do depending on the actual data.
strcmp returns 0 if they exactly match! you would need to ensure your data string has a NULL terminating character or you could use
strncmp(data, "EDC1", 4) this will only check the first 4 characters.

So if you know what the characters are that you are recieving then you can check those. If the data will change then you have 2 options. Convert the data so you can do range checking e.g.
if (inVal > 0 && inVal < 100)
of have multiple string comparisons.

hope this helps,
Wayne
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