Skip to content

Commit 8e3d27a

Browse files
String: Add fromUtf8ToEAscii.
1 parent 844e4bf commit 8e3d27a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: api/String.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -753,4 +753,38 @@ double String::toDouble(void) const
753753
return 0;
754754
}
755755

756+
String String::fromUtf8ToEAscii(void) const
757+
{
758+
/*
759+
* references :
760+
* https://www.ime.usp.br/~pf/algoritmos/apend/unicode.html
761+
* https://www.utf8-chartable.de/unicode-utf8-table.pl
762+
* https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
763+
*/
764+
String eAsciiString;
765+
for (uint8_t i=0; i< length(); i++)
766+
{
767+
uint8_t byteScope = (uint8_t)buffer[i];
768+
if (byteScope < 0x7F)
769+
{
770+
eAsciiString += buffer[i];
771+
}
772+
else if (byteScope == 0xC3)
773+
{
774+
i++;
775+
byteScope = (uint8_t)buffer[i];
776+
if (byteScope > 0x7F)
777+
eAsciiString += (char)(byteScope+0x40);
778+
}
779+
else if (byteScope == 0xC2)
780+
{
781+
i++;
782+
byteScope = (uint8_t)buffer[i];
783+
if (byteScope > 0x7F)
784+
eAsciiString += (char)(byteScope);
785+
}
786+
}
787+
return eAsciiString;
788+
}
789+
756790
} // namespace arduino

Diff for: api/String.h

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class String
218218
long toInt(void) const;
219219
float toFloat(void) const;
220220
double toDouble(void) const;
221+
String fromUtf8ToEAscii(void) const;
221222

222223
protected:
223224
char *buffer; // the actual char array

0 commit comments

Comments
 (0)