Matheus Toniolli
Matheus Toniolli

Reputation: 478

How to convert a String to a char * in Arduino?

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.

char *ToCharHEX(int x)
{
    String s;
    int y = 0;
    int z = 1;
    do
    {

        if (x > 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 > 16 || y * 16 == z);
    char *c;
    s.toCharArray(c, s.length());
    Serial.print(c);
    return c;
}

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.

Upvotes: 3

Views: 27289

Answers (2)

Nihara Mayurawasala
Nihara Mayurawasala

Reputation: 21

char* string2char(String command){
    if(command.length()!=0){
        char *p = const_cast<char*>(command.c_str());
        return p;
    }
}

Upvotes: 2

Phillip Elm
Phillip Elm

Reputation: 2194

Updated: Your Question re: String -> char* conversion:

String.toCharArray(char* buffer, int length) wants a character array buffer and the size of the buffer.

Specifically - your problems here are that:

  1. char* c is a pointer that is never initialized.
  2. length is supposed be be the size of the buffer. The string knows how long it is.

So, a better way to run this would be:

char c[20];
s.toCharArray(c, sizeof(c));

Alternatively, you could initialize c with malloc, but then you'd have to free it later. Using the stack for things like this saves you time and keeps things simple.

Reference: https://www.arduino.cc/en/Reference/StringToCharArray


The intent in your code:

This is basically a duplicate question of: https://stackoverflow.com/a/5703349/1068537

See Nathan's linked answer:

// using an int and a base (hexadecimal):
stringOne =  String(45, HEX);   
// prints "2d", which is the hexadecimal version of decimal 45:
Serial.println(stringOne);  

Unless this code is needed for academic purposes, you should use the mechanisms provided by the standard libraries, and not reinvent the wheel.

  • String(int, HEX) returns the hex value of the integer you're looking to convert
  • Serial.print accepts String as an argument

Upvotes: 6

Related Questions