我正在做一個函數在Arduino中將一個整數轉換成十六進制char *,但是我遇到了無法將字符串轉換爲char *的問題。也許如果有辦法爲char *動態分配內存,我不需要類String。如何將字符串轉換爲Arduino中的char *?
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;
}
toCharArray()函數不會將字符串轉換爲char數組。 Serial.print(c)正在返回空白打印。我不知道我能做什麼。
my_string.c_str()中的Arduino IDE不行 –
提示整數的十六進制值:什麼是'y,z'的值第一次'int y; int z; while(x> 16 || y * 16 == z)'是否被執行? – chux
my_string.data()或&my_string [0]它將獲得指向內部字符數組的指針。 –