Formatting String to Hex

Hello, I would like to format a string to unsigned long

"0x11" to 0x11

Serial.println("Enter # of bytes to read (i.e. 00000024 in hex:) "); while (Serial.available()==0) { //Wait for user input

} NumOfBytes = Serial.readString();

NumOfBytes1 = Hex + NumOfBytes.substring(0,2); Byte1 = NumOfBytes1.toInt();

Byte1's result is just 0 instead of 0x00.

Byte1 = NumOfBytes1.toInt();

No, Byte1 is just a number and it is stored, like everything else in the computer, in binary. Whether you print it as 0, 00, 0x00, 0x0, or whatever else, it is still the same number 0 represented in the processor as 0b00000000.

If you want it formatted in a certain way when you print it out, like with the 0x and with 2 digits after, then you need to go to google and learn about a function called sprintf.

What OP wants to do, apparently, is convert a String that contains "0x11" to 17 (0x11). The toInt() method won't do that, because it, rightly, expects the string to be a base 10 representation of the value.

OP: Stop using Strings. You can pass a string (which is NOT the same as a String) to strtoul(), which can interpret numbers in any base, to get an unsigned long.