From 8e3d27aad6e493a9d8d6076aa5a657e7c56325a5 Mon Sep 17 00:00:00 2001 From: Junio Date: Tue, 10 Jan 2023 21:25:21 -0300 Subject: [PATCH] String: Add fromUtf8ToEAscii. --- api/String.cpp | 34 ++++++++++++++++++++++++++++++++++ api/String.h | 1 + 2 files changed, 35 insertions(+) diff --git a/api/String.cpp b/api/String.cpp index 0a5c11fe..e03af523 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -753,4 +753,38 @@ double String::toDouble(void) const return 0; } +String String::fromUtf8ToEAscii(void) const +{ + /* + * references : + * https://www.ime.usp.br/~pf/algoritmos/apend/unicode.html + * https://www.utf8-chartable.de/unicode-utf8-table.pl + * https://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + */ + String eAsciiString; + for (uint8_t i=0; i< length(); i++) + { + uint8_t byteScope = (uint8_t)buffer[i]; + if (byteScope < 0x7F) + { + eAsciiString += buffer[i]; + } + else if (byteScope == 0xC3) + { + i++; + byteScope = (uint8_t)buffer[i]; + if (byteScope > 0x7F) + eAsciiString += (char)(byteScope+0x40); + } + else if (byteScope == 0xC2) + { + i++; + byteScope = (uint8_t)buffer[i]; + if (byteScope > 0x7F) + eAsciiString += (char)(byteScope); + } + } + return eAsciiString; +} + } // namespace arduino diff --git a/api/String.h b/api/String.h index 03ecf442..0cce5c7e 100644 --- a/api/String.h +++ b/api/String.h @@ -218,6 +218,7 @@ class String long toInt(void) const; float toFloat(void) const; double toDouble(void) const; + String fromUtf8ToEAscii(void) const; protected: char *buffer; // the actual char array