OBD2 Scan Tool - Converting hex user input to char

Hello,

I am currently working on an OBD2 scan tool for monitoring my cars functions. Overall it is working well, but I am having difficulty with converting user input to usable data.

I have a 2.8" TFT touch screen. On one of the menus, I have a small keyboard in which the user can input what command they want to send to the CAN BUS. for example, the command for transmission temperature is "0x7E1,0x02,0x01,0x3f" I have it set up so that the user types in the command in hex, then my code puts it into a char array.

So, for the command above, the user would type it in, and I would get a char array

char command[8]= {7,e,1,0,2,0,1,3,f}

This is where I get stuck. I then want to use my function sendPID(short, char, char, char). If I were to call this function manually, it would look like this:

sendPid(0x7e1,0x02,0x01,0x3f);//Send and Recieve PID

However, I want to use my char array (command[8]) to call the function. I am looking to do something like this:

char header=command[0]+command[1]+command[2]
char numBytes=command[3]+command[4]
char Mode=command[5]+command[6]
char PID=command[7]+command[7]

So the final function call would be:

sendPID(header,numBytes,Mode,PID) which is the equivalent of sendPid(0x7e1,0x02,0x01,0x3f);

I am not sure how to do this. Is there a simple way to take parts of a char array and construct a short or char?

Thanks in advance.

You appear to be converting a text representation of a hex number into (say) an integer.

// this is too big for a character which is -128 to + 127

char header = command[0]+command[1]+command[2]

// but what you can do is something like this:

int header =  256 * myCharValue( command[0]  )  +  16 * myCharValue( command[1]  )  + myCharValue( command[2]  )  ;

// you have to write function myCharValue( ) which converts a character to a numerical value
// do something like int someInt = someChar - '0'; if the char represents a "digit". 
// or someInt = 10 + someChar - 'a'; if the char represents a "lower case letter" etc.