// Test for normal strings with UTF-8
// Public Domain
char three[] = "3µV";
const char four[] PROGMEM = "4µA";
String five = "5µF";
char six[] = "60€";
String seven = "70€";
char buffer[40];
void setup()
{
Serial.begin( 9600);
Serial.println("\n+++++++++++++++++++++++++++++++++++++++++");
Serial.println("Use a serial terminal that supports UTF-8");
Serial.println(F("1µH")); // Good, text in flash
// copy a string from flash memory to a buffer.
sprintf_P( buffer, PSTR("2µH")); // Good, text in flash
Serial.println( buffer);
// copy a string in ram to a buffer
strcpy( buffer, three); // Good
Serial.println( buffer);
// add one to strlen for the zero terminator
strncpy( buffer, three, strlen(three) + 1); // Good, strlen works with UTF-8 string
Serial.println( buffer);
strcpy_P( buffer, four); // Good, text in flash with PROGMEM
Serial.println( buffer);
// copy a string in flash to buffer byte for byte
for( int i = 0 ; i < sizeof( four) ; i++) // Good, sizeof works with UTF-8 string
{
buffer[i] = pgm_read_byte( four + i);
}
Serial.println( buffer);
Serial.println( five); // Good, a String class with UTF-8 character
Serial.print( "array of char: \"");
Serial.print( six);
Serial.print( "\", strlen=");
Serial.println( strlen(six));
Serial.print( "String object: \"");
Serial.print( seven);
Serial.print( "\", String.length()=");
Serial.println( seven.length());
Serial.println( "+++++++++++++++++++++++++++++++++++++++++");
Serial.println( "Enter a UTF-8 character and press <enter>");
Serial.println( "The hexadecimal value will be displayed.");
}
void loop()
{
if( Serial.available())
{
Serial.print( "You have entered: ");
delay(100); // allow the rest of the line to be received.
while( Serial.available())
{
byte c = Serial.read();
if( c != '\r' && c != '\n') // ignore trailing CR and LF
{
if( c <= 0x0F)
Serial.print( "0");
Serial.print( c, HEX);
Serial.print( ", ");
}
}
Serial.println();
}
}