# how to convert array to integer value in pic c

#### raman00084

##### Full Member level 6
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.

#### betwixt

##### Super Moderator
Staff member
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.

raman00084

Points: 2

### thannara123

Points: 2

#### raman00084

##### Full Member level 6
THANK YOU IT IS WORKING
--- Updated ---

THANK YOU IT IS WORKING
SIR IF I HAVE 4 CHARACTERS HOW TO COMPUTE

Last edited:

#### betwixt

##### Super Moderator
Staff member
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.

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.

#### KlausST

##### Super Moderator
Staff member
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