简体   繁体   English

如何在Arduino中将String转换为char *?

[英]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 *. 我正在做一个将Arduino中的整数转换为十六进制char *的函数,但是遇到了无法将String转换为char *的问题。 Maybe if there is a way to allocate memory dynamically for char * I do not need a class String. 也许如果有一种方法可以为char动态分配内存*我不需要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. toCharArray()函数不会将字符串转换为char数组。 Serial.print (c) is returning empty printing. Serial.print(c)返回空打印。 I do not know what I can do. 我不知道该怎么办。

Updated: Your Question re: String -> char* conversion: 更新:您的问题: String -> char*转换:

String.toCharArray(char* buffer, int length) wants a character array buffer and the size of the buffer. String.toCharArray(char* buffer, int length)一个字符数组缓冲区和该缓冲区的大小。

Specifically - your problems here are that: 具体来说-您的问题在于:

  1. char* c is a pointer that is never initialized. char* c是永远不会初始化的指针。
  2. length is supposed be be the size of the buffer. length应该是缓冲区的大小。 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. 另外,您可以使用malloc初始化c ,但随后必须将其free Using the stack for things like this saves you time and keeps things simple. 将堆栈用于此类事情可以节省您的时间,并使事情变得简单。

Reference: https://www.arduino.cc/en/Reference/StringToCharArray 参考: 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 这基本上是一个重复的问题: 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 String(int, HEX)返回您要转换的整数的十六进制值
  • Serial.print accepts String as an argument Serial.print接受String作为参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM