Arduino에서 int를 문자열로 변환하는 방법은 무엇입니까?
int, n
를 문자열 로 변환 하여 직렬로 보낼 때 문자열로 보내도록 하려면 어떻게해야 합니까?
이것이 내가 지금까지 가지고있는 것입니다.
int ledPin=13;
int testerPin=8;
int n=1;
char buf[10];
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(testerPin, OUTPUT);
Serial.begin(115200);
}
void loop()
{
digitalWrite(ledPin, HIGH);
sprintf(buf, "Hello!%d", n);
Serial.println(buf);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
n++;
}
다음과 같이 사용하십시오.
String myString = String(n);
여기에서 더 많은 예를 찾을 수 있습니다 .
에 itoa()
포함 된 기능 사용stdlib.h
char buffer[7]; //the ASCII of the integer will be stored in this char array
itoa(-31596,buffer,10); //(integer, yourBuffer, base)
간단하게 다음을 수행 할 수 있습니다.
Serial.println(n);
n
ASCII 문자열로 자동 변환 됩니다. 에 대한 설명서를Serial.println()
참조하십시오 .
다음과 같이 String 객체를 감싸기 만하면됩니다.
String numberString = String(n);
다음을 수행 할 수도 있습니다.
String stringOne = "Hello String"; // using a constant String
String stringOne = String('a'); // converting a constant char into a String
String stringTwo = String("This is a string"); // converting a constant string into a String object
String stringOne = String(stringTwo + " with more"); // concatenating two strings
String stringOne = String(13); // using a constant integer
String stringOne = String(analogRead(0), DEC); // using an int and a base
String stringOne = String(45, HEX); // using an int and a base (hexadecimal)
String stringOne = String(255, BIN); // using an int and a base (binary)
String stringOne = String(millis(), DEC); // using a long and a base
이것은 int (부호있는 16 비트 정수)를 문자열로 변환하기위한 속도 최적화 솔루션입니다.
This implementation avoids using division since 8-bit AVR used for Arduino has no hardware DIV instruction, the compiler translate division into time-consuming repetitive subtractions. Thus the fastest solution is using conditional branches to build the string.
A fixed 7 bytes buffer prepared from beginning in RAM to avoid dynamic allocation. Since it's only 7 bytes, the cost of fixed RAM usage is considered minimum. To assist compiler, we add register modifier into variable declaration to speed-up execution.
char _int2str[7];
char* int2str( register int i ) {
register unsigned char L = 1;
register char c;
register boolean m = false;
register char b; // lower-byte of i
// negative
if ( i < 0 ) {
_int2str[ 0 ] = '-';
i = -i;
}
else L = 0;
// ten-thousands
if( i > 9999 ) {
c = i < 20000 ? 1
: i < 30000 ? 2
: 3;
_int2str[ L++ ] = c + 48;
i -= c * 10000;
m = true;
}
// thousands
if( i > 999 ) {
c = i < 5000
? ( i < 3000
? ( i < 2000 ? 1 : 2 )
: i < 4000 ? 3 : 4
)
: i < 8000
? ( i < 6000
? 5
: i < 7000 ? 6 : 7
)
: i < 9000 ? 8 : 9;
_int2str[ L++ ] = c + 48;
i -= c * 1000;
m = true;
}
else if( m ) _int2str[ L++ ] = '0';
// hundreds
if( i > 99 ) {
c = i < 500
? ( i < 300
? ( i < 200 ? 1 : 2 )
: i < 400 ? 3 : 4
)
: i < 800
? ( i < 600
? 5
: i < 700 ? 6 : 7
)
: i < 900 ? 8 : 9;
_int2str[ L++ ] = c + 48;
i -= c * 100;
m = true;
}
else if( m ) _int2str[ L++ ] = '0';
// decades (check on lower byte to optimize code)
b = char( i );
if( b > 9 ) {
c = b < 50
? ( b < 30
? ( b < 20 ? 1 : 2 )
: b < 40 ? 3 : 4
)
: b < 80
? ( i < 60
? 5
: i < 70 ? 6 : 7
)
: i < 90 ? 8 : 9;
_int2str[ L++ ] = c + 48;
b -= c * 10;
m = true;
}
else if( m ) _int2str[ L++ ] = '0';
// last digit
_int2str[ L++ ] = b + 48;
// null terminator
_int2str[ L ] = 0;
return _int2str;
}
// Usage example:
int i = -12345;
char* s;
void setup() {
s = int2str( i );
}
void loop() {}
This sketch is compiled to 1,082 bytes of code using avr-gcc which bundled with Arduino v1.0.5 (size of int2str function itself is 594 bytes). Compared with solution using String object which compiled into 2,398 bytes, this implementation can reduce your code size by 1.2 Kb (assumed that you need no other String's object method, and your number is strict to signed int type).
This function can be optimized further by writing it in proper assembler code.
The solution is much too big. Try this simple one. Please provide a 7+ character buffer, no check made.
char *i2str(int i, char *buf){
byte l=0;
if(i<0) buf[l++]='-';
boolean leadingZ=true;
for(int div=10000, mod=0; div>0; div/=10){
mod=i%div;
i/=div;
if(!leadingZ || i!=0){
leadingZ=false;
buf[l++]=i+'0';
}
i=mod;
}
buf[l]=0;
return buf;
}
Can be easily modified to give back end of buffer, if you discard index 'l' and increment the buffer directly.
Here below is a self composed myitoa() which is by far smaller in code, and reserves a FIXED array of 7 (including terminating 0) in char *mystring, which is often desirable. It is obvious that one can build the code with character-shift instead, if one need a variable-length output-string.
void myitoa(int number, char *mystring) {
boolean negative = number>0;
mystring[0] = number<0? '-' : '+';
number = number<0 ? -number : number;
for (int n=5; n>0; n--) {
mystring[n] = ' ';
if(number > 0) mystring[n] = number%10 + 48;
number /= 10;
}
mystring[6]=0;
}
Serial.println(val)
Serial.println(val, format)
for more you can visit to the site of arduino https://www.arduino.cc/en/Serial/Println
wish this will help you. thanks!
in the first example, a classical C string is being used: it is just an array of characters terminated by a zero (NUL character). other examples use the String class, a C++ class that I would steer far from except from toy examples and tiny demos. It leaks memory in ways that are not so easy to predict, and leads to mysterious lockups of the board.
참고URL : https://stackoverflow.com/questions/7910339/how-to-convert-int-to-string-on-arduino
'IT박스' 카테고리의 다른 글
Scala 자체에서 Scala 버전을 얻으려면 어떻게해야합니까? (0) | 2020.09.24 |
---|---|
Mac OSX의 Python 위치 (0) | 2020.09.24 |
Java 생성자에서 int 배열 초기화 (0) | 2020.09.24 |
마크 다운 셀 ipython / jupyter 노트북에서 색상이 어떻게 변경됩니까? (0) | 2020.09.24 |
Log4j2 구성-log4j2 구성 파일을 찾을 수 없습니다. (0) | 2020.09.24 |