title: How to convert a String to a char * in Arduino?
body: <p>I'm doing a function to convert an integer into a hexadecimal char * in Arduino, but I came across the problem of not being able to convert a String to a char *. Maybe if there is a way to allocate memory dynamically for char * I do not need a class String.</p>
  
  <pre><code>char *ToCharHEX(int x)
  {
      String s;
      int y = 0;
      int z = 1;
      do
      {
  
          if (x &gt; 16)
          {
              y = (x - (x % 16)) / 16;
              z = (x - (x % 16));
              x = x - (x - (x % 16));
          }
          else
          {
              y = x;
          }
          switch (y)
          {
          case 0:
              s += "0";
              continue;
          case 1:
              s += "1";
              continue;
          case 2:
              s += "2";
              continue;
          case 3:
              s += "3";
              continue;
          case 4:
              s += "4";
              continue;
          case 5:
              s += "5";
              continue;
          case 6:
              s += "6";
              continue;
          case 7:
              s += "7";
              continue;
          case 8:
              s += "8";
              continue;
          case 9:
              s += "9";
              continue;
          case 10:
              s += "A";
              continue;
          case 11:
              s += "B";
              continue;
          case 12:
              s += "C";
              continue;
          case 13:
              s += "D";
              continue;
          case 14:
              s += "E";
              continue;
          case 15:
              s += "F";
              continue;
          }
      }while (x &gt; 16 || y * 16 == z);
      char *c;
      s.toCharArray(c, s.length());
      Serial.print(c);
      return c;
  }
  </code></pre>
  
  <p>The toCharArray () function is not converting the string to a char array. Serial.print (c) is returning empty printing. I do not know what I can do.</p>