converting Hex digits in a String to a byte

I have a String... String MyString ="88 FF A1"; (the actual string is long, coming from my ESP32 OBD) I use .substring(x,x) to extract parts of String = "88" or "FF" or "A1"...

How does one now make this into a byte... sting"FF" into byte FF...

I need to do operations on the Byte using the bitRead(MyByte, "0 to 7").

This should not take huge amounts of code to accomplish as the "string" already represents a Hex number.

Any help on this would gratefully be appreciated...

Do not use the "String" class. You can convert a cstring with hexadecimal to an integer with strtol.

You may like this practice:

char str[] = "3B"; //0x33(0x30 + 3), 0x42(0x37 + B)
byte x, x1, x2;

void setup()
{
  Serial.begin(9600);
  if (str[0] > 0x37)
  {
    x1 = str[0] - 0x37; 
    x1 = x1 << 4;   
  }
  else
  {
    x1 = str[0] << 4;   //30
  }

  if (str[1] > 0x37)
  {
    x2 = str[1] - 0x37;  //0B
  }
  else
  {
    x2 = str[1] & 0x0F;
  }

  x = x1 | x2;   //3B
  Serial.print(x, HEX);
}

void loop()
{

}

Thanks you guy's but,,,

Wow,,, That would be great, except that I'm living in the year 2018, surely there has to be smaller code..??

Like..... x = strToHex(mydata)...

I have this:

String MyData = "88 FF A1 02 34"; String XString; byte MyByte;

void Prosses_HeaderData() {

XString = MyData.substring(ByteCount,ByteCount + 2); // This gives me "FF"

THIS IS WHERE I NEED TO put XString into a Byte form..........

Serial.print(" Headers = "); // Num of Headers to follow

if (bitRead(MyByte, 4) == 0) { Serial.print("3"); } else { Serial.print("1"); }

Now, I'm not sure about Cstrings is that like... char XString; or chr *XString; because I just tried that and got error "invalid conversion ...."

OK, so for others searching to understand this language, GolamMostafa code works great but sure is long,, Shame on C or C++ developers to have 101 different ways to wright a string then not to implement a simple conversion.. At least take GolamMostafa code and make a library function.... Thanks Danois90 and GolamMostafa for the Quick response.. What comes around goes around and it's a wonderful world to have people like you to help others...

What @Golam gave you is the long way round to do it for sure.

You had it right here:

Like..... x = strToHex(mydata)...

Danois90: Do not use the "String" class. You can convert a cstring with hexadecimal to an integer with strtol.

That's all you need is strtol.

OMG, Delta_G, I just made that up in my head (x = strToHex(mydata)...) can you show how that could be implemented using strtol...

Don_Warr: OMG, Delta_G, I just made that up in my head (x = strToHex(mydata)...) can you show how that could be implemented using strtol...

Can't you look up strtol?

Here, in case google isn't working for you:

http://www.cplusplus.com/reference/cstdlib/strtol/

It works pretty much like you guessed it would. There are examples on that page.