Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,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
1 change: 1 addition & 0 deletions api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,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
Expand Down