mirror of
https://github.com/godotengine/godot.git
synced 2025-10-19 16:03:29 +00:00
add hex_decode()
to String
This commit is contained in:
parent
44d539465a
commit
ca0feabbb0
6 changed files with 88 additions and 0 deletions
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue