There are easier ways.
The first point is the number can't be completely converted until it is known that all of the number has arrived.
A common way to do this is to receive a character that isn't part of the number. For the serial monitor that is newline or carriage return. So parsing character by character doesn't gain much.
Secondly it is straightforward to write code which converts from characters to a binary number without assuming much about the number (other than it will fit into an int).
Third, it is easier to debug if the whole line of text is collected into an array, and then 'decoded'. It is easy to add a print statement which echos the value read so far, and print statements which show the processing steps.
FInally, the character codes for '0' to '9', 'A' to 'F', or 'a' to 'f', are not conveniently arranged to be the value 0 to 9, and 10 to 15. They are ASCII (or the first 127 of UTF-8) codes, and so they need to be converted to the correct binary value.
This hasn't been tested, and isn't exactly the way I would do it, however, it might help you:
// forum topic 74377
#include <string.h>
#include <ctype.h>
int inByte;
char line[20];
int linePos = 0;
const char prefix[] = "PRS ";
// End of String, i.e. the 0 byte.
const char EOS = 0;
// The forum software consumes '\ followed by 0' which breaks code
// So this kludge reduces that problem.
int hexValue;
int nibble;
void setup() {
Serial1.begin(57600);
}
void loop() {
if (Serial1.available()) {
inByte = Serial1.read();
SerialUSB.print(inByte, BYTE);
}
if (inByte == '\n' || inByte == '\r') { // got a line?
line[linePos] = EOS; // reducing horriblness of forum S/W
if (strncmp(prefix, line, sizeof(prefix)) == 0) { // matches prefix?
linePos = strlen(prefix); // skip over prefix
hexValue = 0;
while (isxdigit(line[linePos])) { // 0-9, A-Z, a-z?
if ('A' <= line[linePos] && line[linePos] <= 'F') {
nibble = (line[linePos]-'A')+10;
} else if ('a' <= line[linePos] && line[linePos] <= 'z') {
nibble = (line[linePos]-'a')+10;
} else if ('0' <= line[linePos] && line[linePos] <= '9') {
nibble = (line[linePos]-'0'); // Edit: deleted "+10"
}
hexValue = (hexValue << 4) + nibble;
}
SerialUSB.print("value of line='");
SerialUSB.print(line);
SerialUSB.print("' value of number=");
SerialUSB.print(hexValue);
SerialUSB.print(" in hex=");
SerialUSB.println(hexValue, HEX);
}
linePos = 0; // get ready for another line
} else if (linePos < sizeof(line)-1) { // append to line
line[linePos] = inByte;
linePos++;
}
}
WARNING: This has been compiled but not tested.