Last active
August 15, 2025 02:54
-
-
Save ouoam/238c47a5ce2ba28a12745e6237546de3 to your computer and use it in GitHub Desktop.
Convert Unicode to UTF-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Convert Unicode to UTF-8 | |
| static inline String TinyGsmDecodeHex16bitUTF8(String& instr) { | |
| String result; | |
| for (uint16_t i = 0; i < instr.length(); i += 4) { | |
| char buf[5] = {0,0,0,0,0}; | |
| buf[0] = instr[i]; | |
| buf[1] = instr[i + 1]; | |
| buf[2] = instr[i + 2]; | |
| buf[3] = instr[i + 3]; | |
| uint16_t b = strtol(buf, nullptr, 16); | |
| if (b < 0x0080) { | |
| result += char(b); | |
| } else if (b < 0x0800) { | |
| result += char(0xC0 | ((b >> 6 ) & 0x1F)); | |
| result += char(0x80 | ( b & 0x3F)); | |
| } else { | |
| result += char(0xE0 | ((b >> 12) & 0x0F)); | |
| result += char(0x80 | ((b >> 6 ) & 0x3F)); | |
| result += char(0x80 | ( b & 0x3F)); | |
| } | |
| // Ignore 4 byte because 16 bit is use only 3 byte | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment