# Converting The value receive from hex string to Decimal

Hello,

I am try to convert the HEX String to Decimal value ..
for an example ,

int Byte 1 : 7A
int Byte 2 :2A

String x= 7A2A

i need to convert this value to decimal value..thank you...

If you use a nul terminated character array instead of String with capital S, you can use strtol.

``````void setup()
{
char input[] = "7A2A";

int x;
char *endptr;

x = strtol(input, &endptr, 16);

Serial.begin(250000);
Serial.println(x, HEX);
Serial.println(x, DEC);
}

void loop()
{
}
``````

Output

``````7A2A
31274
``````

You can use endptr to validate the input; the value that it points to should be NULL (the nul terminating character at the end of input) if the string is valid.

``````void setup()
{
char input[] = "7A2Z";

int x;
char *endptr;

x = strtol(input, &endptr, 16);

Serial.begin(250000);

Serial.print("address of input = "); Serial.println((int)input);
Serial.print("endptr points to "); Serial.println((int)endptr);
Serial.print("value at endptr = "); Serial.println((char)*endptr, HEX);
if (*endptr == NULL)
{
Serial.println("input is valid");
Serial.println(x, HEX);
Serial.println(x, DEC);
}
else
{
Serial.println("input is invalid");
}
}
``````

The output

``````address of input = 2287
endptr points to 2290
value at endptr = 5A
input is invalid
``````