summaryrefslogtreecommitdiffstats
path: root/core/string/ustring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/string/ustring.cpp')
-rw-r--r--core/string/ustring.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index 6a59942a56..773445edb6 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -1644,6 +1644,35 @@ String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
return ret;
}
+Vector<uint8_t> String::hex_decode() const {
+ ERR_FAIL_COND_V_MSG(length() % 2 != 0, Vector<uint8_t>(), "Hexadecimal string of uneven length.");
+
+#define HEX_TO_BYTE(m_output, m_index) \
+ uint8_t m_output; \
+ c = operator[](m_index); \
+ if (is_digit(c)) { \
+ m_output = c - '0'; \
+ } else if (c >= 'a' && c <= 'f') { \
+ m_output = c - 'a' + 10; \
+ } else if (c >= 'A' && c <= 'F') { \
+ m_output = c - 'A' + 10; \
+ } else { \
+ ERR_FAIL_V_MSG(Vector<uint8_t>(), "Invalid hexadecimal character \"" + chr(c) + "\" at index " + m_index + "."); \
+ }
+
+ Vector<uint8_t> out;
+ int len = length() / 2;
+ out.resize(len);
+ for (int i = 0; i < len; i++) {
+ char32_t c;
+ HEX_TO_BYTE(first, i * 2);
+ HEX_TO_BYTE(second, i * 2 + 1);
+ out.write[i] = first * 16 + second;
+ }
+ return out;
+#undef HEX_TO_BYTE
+}
+
void String::print_unicode_error(const String &p_message, bool p_critical) const {
if (p_critical) {
print_error(vformat("Unicode parsing error, some characters were replaced with spaces: %s", p_message));