Demo-Code for retrieving the ASCII-codes of characters with multibyte (UTF-8) encoding [code example]

I guess for some people this code could be useful

// some more special characters on non-US-keyboards have a multibyte encoding
// this means the character is encoded with two or even three bytes
// this demo-code shows how the ASCII-code-byte-valueS of such a character can be written to the serial monitor
// the code uses the SafeString-library for easier string-handling
// the SafeString-library can be donwloaded with the library-manager of the Arduino_IDE

#include <SafeString.h>
createSafeString(myDemo_SS, 256);

#define Button_D2 15

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );;
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Setup-Start");
  PrintFileNameDateTime();

  pinMode(Button_D2, INPUT_PULLUP);
  // if you don't have a button handy remove the comment-signes here
  // and delete the   if (digitalRead(Button_D2) == LOW) { code
  // from loop
  //printByteValues();
}

void printByteCodes() {
  byte myByte;

  Serial.print("myDemo_SS ");
  Serial.println(myDemo_SS);

  for (int i = 0; i <= myDemo_SS.length(); i++) {
    myByte = myDemo_SS[i];
    Serial.print("myByte ");
    Serial.println(myByte);    
  }
  Serial.println("\n");    
}


void printByteValues() {
  
  myDemo_SS  = "ä";
  printByteCodes(); 

  myDemo_SS  = "ö";
  printByteCodes(); 

  myDemo_SS  = "ü";
  printByteCodes(); 

  myDemo_SS  = "Ä";
  printByteCodes(); 

  myDemo_SS  = "Ö";
  printByteCodes(); 

  myDemo_SS  = "Ü";
  printByteCodes(); 

  myDemo_SS  = "µ";
  printByteCodes(); 

  myDemo_SS  = "²";
  printByteCodes(); 

  myDemo_SS  = "³";
  printByteCodes(); 

  myDemo_SS  = "°";
  printByteCodes(); 

  myDemo_SS  = "§";
  printByteCodes(); 

  myDemo_SS  = "ß";
  printByteCodes(); 

  myDemo_SS  = "€";
  printByteCodes(); 

  myDemo_SS  = "|";
  printByteCodes(); 

}


void loop() {

  if (digitalRead(Button_D2) == LOW) {
    delay(200); // quick and dirty button-debouncing
    printByteValues();
    delay(200);
  }
}

brs

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.