Saturday, March 22, 2014

Convert int to String

This example show how to convert int to String in various number base, BIN, HEX, DEC.
void setup() {
  
}

void loop() {
  int i = 1023;
  Serial.println("http://arduino-er.blogspot.com/");
  Serial.println("Test String: convert int to String");
  Serial.println();
  
  Serial.println("String(i) = " + String(i));
  Serial.println();
  
  Serial.println("String(i, BIN) = " 
      + String(i, BIN));
  Serial.println("String(i, HEX) = " 
      + String(i, HEX));
  Serial.println("String(i, DEC) = " 
      + String(i, DEC));
  Serial.println();
  
  Serial.println("String(i+i, BIN) = " 
      + String(i+i, BIN));
  Serial.println("String(i+i, HEX) = " 
      + String(i+i, HEX));
  Serial.println("String(i+i, DEC) = " 
      + String(i+i, DEC));
  Serial.println();
  
  Serial.println("String(i, BIN)+String(i, BIN) = " 
      + String(i, BIN)+String(i, BIN));
  Serial.println("String(i, HEX)+String(i, HEX) = " 
      + String(i, HEX)+String(i, HEX));
  Serial.println("String(i, DEC)+String(i, DEC) = " 
      + String(i, DEC)+String(i, DEC));
  Serial.println();
  
  delay(1000);
}

Convert int to String
Convert int to String

1 comment:

  1. Nice concise code. Do you have similar for the reverse, String to int?
    Presumably you would use isDigit() and toInt().

    ReplyDelete