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 support@ccsinfo.com

converting hexadecimal to decimal value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 35

View user's profile Send private message

converting hexadecimal to decimal value
PostPosted: Sat Jul 24, 2021 1:55 am     Reply with quote

char gps_buf[]={"GPGGA,000044.262,,,,,0,0,,,M,,M,,*4E"};

in the array the last 2 characters is the check sum value ( 4E)

HOW CAN I CONVERT THE LAST 2 CHARACTERS TO DECIMAL VALUE
FOR EXAMPLE
CHAR CHECK_SUM[3]={0}
CHECK_SUM[0]='4';
CHECK_SUM[1]='E';

HOW TO CONVERT THIS TO DECIMAL VALUE THAT IS 78
KINDLY HELP.
temtronic



Joined: 01 Jul 2010
Posts: 8147
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jul 24, 2021 4:39 am     Reply with quote

quick reply..
CCS supplies the function ATOI().

If you open the manual, it's listed and the example program 'input.c' shows how to use it.

jay
Ttelmah



Joined: 11 Mar 2010
Posts: 17318

View user's profile Send private message

PostPosted: Sat Jul 24, 2021 10:45 am     Reply with quote

Though atoi is supplied, it is quite a complex way of doing the conversion.
Easier to just use a simple convert and multiply on the characters. So:
Code:

int htodec(char c)
{
    if(c >= '0' && c<= '9')
       return c-'0';
    if(c >= 'A' && c <= 'F')
       return c-55;
    if (c >= 'a' && c <= 'f')
       return c-87;
    return 0; //Impossible character
}

//then for the locations of the '4E':

val=htodec(gps_buf[34])*16+htodec(gps_buf[35]);


Should give the value in decimal.
I think I've counted the locations correctly (do check...).

This is faster and smaller than using atoi
temtronic



Joined: 01 Jul 2010
Posts: 8147
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 5:13 am     Reply with quote

Hey Mr. T....
Using Notepad+, I get 35, and 36 as the locations.
BUT...that's really 34 and 35 as we have to start at ZERO for arrays.

In QB45 you could use either '0' or '1' as the starting cell.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 17318

View user's profile Send private message

PostPosted: Sun Jul 25, 2021 6:57 am     Reply with quote

Cor, I counted right!... Very Happy
I was just counting characters on a laptop.
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