# Convert hex string to int

**URL:** <https://forum.arduino.cc/t/convert-hex-string-to-int/301014>\
**Category:** Programming\
**Created:** [March 28, 2015, 8:56pm UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014 "2015-03-28T20:56:39Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 21, 2015, 6:50am UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/21 "2015-04-21T06:50:40Z")

</div>

Jack's code works for me.  
Please copy the code that you are using and post it here.

---

<div class="post-metadata">

**Author:** ![davros](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@davros](https://forum.arduino.cc/u/davros)\
**Post date:** [April 21, 2015, 7:21am UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/22 "2015-04-21T07:21:06Z")

</div>

I used the select function in his original thread and I haven't changed anything.

```cpp
void setup() {
  Serial.begin(9600);
}

void loop() {
  char input[3];
  int charsRead;
  int val;
  
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', input, 2);  // fetch the two characters
    input[charsRead] = '\0';                            // Make it a string

    val = StrToHex(input);                              // Convert it

    Serial.print("Hex input was: ");                    // Show it...
    Serial.print(input);
    Serial.print("  decimal = ");
    Serial.println(val);
  }
}

int StrToHex(char str[])
{
  return (int) strtol(str, 0, 16);
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 21, 2015, 7:33am UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/23 "2015-04-21T07:33:15Z")

</div>

Works for me.  
Can I please clarify what you are entering ? Every keystroke or mouse click in the order you enter them.

---

<div class="post-metadata">

**Author:** ![davros](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@davros](https://forum.arduino.cc/u/davros)\
**Post date:** [April 21, 2015, 8:55am UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/24 "2015-04-21T08:55:04Z")

</div>

I've got the serial monitor set to autoscroll, no line ending, 9600Baud - tried newline as well. I've tried entering a few different codes in both uppercase and lowercase.

Typing the each of the below and either hitting the enter key or pushing the send button.  
AF  
FF  
ff  
2b

This is running on a Teensy 3.1 using Arduino 1.6.1 and teensyduino as the loader.

ended up using sscanf for the conversion.

```cpp
sscanf(dataFromPC, "%x", &integerFromPC);  //converts HEX string to a DEC int

```

---

<div class="post-metadata">

**Author:** ![nawatat1112](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nawatat1112](https://forum.arduino.cc/u/nawatat1112)\
**Post date:** [August 26, 2016, 7:12am UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/25 "2016-08-26T07:12:10Z")

</div>

> econjack:  
> If it is truly a 2 char array that holds the hex string, then try this:
> 
> ```arduino
> void setup() {
> 
> ```

Serial.begin(9600);  
}

void loop() {  
&nbsp; char input[3];  
&nbsp; int charsRead;  
&nbsp; int val;  
&nbsp;  
&nbsp; if (Serial.available() \> 0) {  
&nbsp; &nbsp; charsRead = Serial.readBytesUntil('\n', input, 2);&nbsp; // fetch the two characters  
&nbsp; &nbsp; input[charsRead] = '\0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Make it a string

val = StrToHex(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Convert it

Serial.print("Hex input was: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Show it...  
&nbsp; &nbsp; Serial.print(input);  
&nbsp; &nbsp; Serial.print("&nbsp; decimal = ");  
&nbsp; &nbsp; Serial.println(val);  
&nbsp; }  
}

int StrToHex(char str[])  
{  
&nbsp; return (int) strtol(str, 0, 16);  
}

> ```arduino
> 
> I'm not a big fan of *sscanf()* because it eats more memory than is usually necessary.
> 
> ```

Can you help me for substring function Ex eFA covert FA to Hex

---

<div class="post-metadata">

**Author:** ![Vicne](https://avatars.discourse-cdn.com/v4/letter/v/85e7bf/32.png) [@Vicne](https://forum.arduino.cc/u/Vicne)\
**Post date:** [May 10, 2018, 2:55pm UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/26 "2018-05-10T14:55:38Z")

</div>

Hi,

Sorry for waking up a dead thread, but UKHeliBob's nice and simple solution (which I just used) contains a typo which could go unnoticed and bite you later on.  
Namely the digit conversions should read \<='9', not \<'9'.  
Here's a fixed version:

```cpp
char hexValue[] = {'F', '9'};

void setup()
{
  Serial.begin(115200);

  byte tens = (hexValue[0] <= '9') ? hexValue[0] - '0' : hexValue[0] - '7';
  byte ones = (hexValue[1] <= '9') ? hexValue[1] - '0' : hexValue[1] - '7';
  byte number = (16 * tens) + ones;
  Serial.println(number);
  Serial.println(number, HEX);
}

void loop()
{
}

```

KR,  
Vicne

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:12pm UTC](https://forum.arduino.cc/t/convert-hex-string-to-int/301014/27 "2021-05-05T21:12:23Z")

</div>

Closed on May 5, 2021, 9:12pm UTC



[Previous page](https://forum.arduino.cc/t/convert-hex-string-to-int/301014.md?page=1)
