Arduino의 아날로그 핀 중 하나에서 int 값을 얻습니다. 어떻게이을 연결 않는 String
한 다음 변환 String
A를 char[]
?
을 (를) 시도하도록 제안 char msg[] = myString.getChars();
되었지만 getChars
존재하지 않는 메시지를 받고 있습니다.
답변
-
정수를 변환하고 추가하려면 연산자 + = (또는 멤버 함수
concat
)를 사용하십시오.String stringOne = "A long integer: "; stringOne += 123456789;
-
유형으로 문자열을 얻으려면
char[]
, 사용 toCharArray ()를 :char charBuf[50]; stringOne.toCharArray(charBuf, 50)
이 예에서는 49 자 (널로 끝나는 것으로 가정)를위한 공간 만 있습니다. 크기를 동적으로 만들 수 있습니다.
간접비
반입 비용 String
(스케치에서 사용하지 않는 경우 포함되지 않음)은 약 1212 바이트 프로그램 메모리 (플래시)와 48 바이트 RAM입니다.
이것은 Arduino Leonardo 스케치를 위해 Arduino IDE 버전 1.8.10 (2019-09-13)을 사용하여 측정되었습니다 .
답변
그냥 참고로, 여기 사이의 변환하는 방법의 예입니다 String
및 char[]
동적 길이는 –
// Define
String str = "This is my string";
// Length (with one extra character for the null terminator)
int str_len = str.length() + 1;
// Prepare the character array (the buffer)
char char_array[str_len];
// Copy it over
str.toCharArray(char_array, str_len);
예, 이것은 유형 변환과 같은 간단한 것에 대해 고통스럽게 둔감하지만 슬프게도 가장 쉬운 방법입니다.
답변
다음을 사용하여 수정 가능한 문자열이 필요하지 않은 경우 char *로 변환 할 수 있습니다.
(char*) yourString.c_str();
이것은 arduino에서 MQTT를 통해 String 변수를 게시하려는 경우 매우 유용합니다.
답변
그 어떤 것도 효과가 없었습니다. 훨씬 더 간단한 방법이 있습니다. str 레이블은 배열이 무엇인지에 대한 포인터입니다.
String str = String(yourNumber, DEC); // Obviously .. get your int or byte into the string
str = str + '\r' + '\n'; // Add the required carriage return, optional line feed
byte str_len = str.length();
// Get the length of the whole lot .. C will kindly
// place a null at the end of the string which makes
// it by default an array[].
// The [0] element is the highest digit... so we
// have a separate place counter for the array...
byte arrayPointer = 0;
while (str_len)
{
// I was outputting the digits to the TX buffer
if ((UCSR0A & (1<<UDRE0))) // Is the TX buffer empty?
{
UDR0 = str[arrayPointer];
--str_len;
++arrayPointer;
}
}
답변
