# String to int

**URL:** <https://forum.arduino.cc/t/string-to-int/688250>\
**Category:** Programming\
**Created:** [December 23, 2020, 12:53pm UTC](https://forum.arduino.cc/t/string-to-int/688250 "2020-12-23T12:53:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swenw](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@swenw](https://forum.arduino.cc/u/swenw)\
**Post date:** [December 23, 2020, 12:53pm UTC](https://forum.arduino.cc/t/string-to-int/688250/1 "2020-12-23T12:53:17Z")

</div>

Hi, i got stuck parsing a Hex string to an integer. I tried the following:

```cpp
String keybChars ="FF01";
  for(int i=0; i<4; i++)
  {
    Serial.print(keybChars[i]);
    int tmp =  (int)strtol(keybChars[i], NULL, 16);
    Serial.println(tmp, DEC);
  }

```

put the output is:

```cpp
F0
F0
00
10

```

Can someone please tell me what im doing wrong? Or tell me a better way to parse a String in HEX format to an integer?

Thank you!

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [December 23, 2020, 12:55pm UTC](https://forum.arduino.cc/t/string-to-int/688250/2 "2020-12-23T12:55:20Z")

</div>

Your problem is that strtol expects a string, but you're trying to pass it a single character.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [December 23, 2020, 4:24pm UTC](https://forum.arduino.cc/t/string-to-int/688250/3 "2020-12-23T16:24:14Z")

</div>

The variable type **S** tring can cause memory-problems so you should'nt use the variable type **S** tring at all

```cpp
char keybChars[4+1]; // 4 characters plus one byte for terminating zero
keyChars = "FF01";

```

What is not clear to me what do you want the integer to contain?  
is your charsequence meant as 4bit hexadecimals or 8bit hexadecimals or do you want to convert to the ASCII-code?  
the further transformation depends on that

best regards Stefan

---

<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 6, 2021, 12:54am UTC](https://forum.arduino.cc/t/string-to-int/688250/4 "2021-05-06T00:54:45Z")

</div>

Closed on May 6, 2021, 12:54am UTC


