Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to convert array to integer value in pic c

Status
Not open for further replies.

raman00084

Full Member level 6
Joined
Nov 29, 2010
Messages
360
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
3,995
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
if the valued are in the range of 0 to 9 then i can use atoi but one value is in hexadecimal
KINDLY HELP.
 

Hexadecimal is base 16 so take the ASCII value of each character, subtract '0' to bring the ASCII value down to units then if the result is more than 9 (meaning it was A-F) subtract an extra 7. Then multiply the first character by 16 and add the second character.
Code:
char DecimalChecksum = 0;
CHECK_SUM[0] -= '0'; if(CHECK_SUM[0] > 9) CHECK_SUM[0] -= 7;
CHECK_SUM[1] -= '0'; if(CHECK_SUM[1] > 9) CHECK_SUM[1] -= 7;
DecimalChecksum = (CHECK_SUM[0] * 16) + CHECK_SUM[1];

Brian.
 
Just change the variable to hold the result to an 'int' because with 4 characters the value can be higher than a char variable can hold.

Then add the lines:
CHECK_SUM[2] -= '0'; if(CHECK_SUM[2] > 9) CHECK_SUM[2] -= 7;
CHECK_SUM[3] -= '0'; if(CHECK_SUM[3] > 9) CHECK_SUM[3] -= 7;

and change the final addition to:
DecimalChecksum = (CHECK_SUM[0] * 4096) + (CHECK_SUM[1] * 256) + (CHECK_SUM[2] * 16) + CHECK_SUM[1];

Note that if you are doing this many times in your program it might be easier to use the sscanf() or strtok() functions but they might use up a lot of additional program memory.

Brian.
 

Hi,

please don´t shout in this forum = don´t use all capatial letters. Read forum rules.

****

Generally:
You don´t want to convert an "array" into an integer. Because "array" just means "multiple" of a variable.
But the variable of an array can be of any variable_type. (Char, int, float, signed or unsigned ...)

What you want to do is: Convert ASCII to binary.
You are not the first one. Thus an internet search gives me more than 16 million results.
(Now we have an additional one...)

Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top