Answer the question
In order to leave comments, you need to log in
How to make string16 from INT in C++?
I have an int variable that contains a number equal to the utf8 character code (for m it will be 1084). Here's how I can make it so that the character that means this number is written to the string16 variable?
Answer the question
In order to leave comments, you need to log in
From documentation:
String class for 16-bit characters.
This is an instantiation of the basic_string class template that uses char16_t as the character type
int code = ...;
std::u16string str("");
str += (char16_t)code;
UTF-8 is a multibyte character set and has no codes. So we consider that we are dealing with Unicode codes.
This is just a piece of my mini-library, so...
The template was used to automatically specialize wchar_t to the correct length, there are actually versions for 1 and 2.
namespace str{
enum {
SURROGATE_MIN = 0xD800,
SURROGATE_MAX = 0xDFFF,
SURROGATE_LO_MIN = SURROGATE_MIN,
SURROGATE_HI_MIN = 0xDC00,
SURROGATE_LO_MAX = SURROGATE_HI_MIN - 1,
SURROGATE_HI_MAX = SURROGATE_MAX,
UNICODE_MAX = 0x10FFFF,
U8_1BYTE_MAX = 0x7F,
U8_2BYTE_MIN = 0x80,
U8_2BYTE_MAX = 0x7FF,
U8_3BYTE_MIN = 0x800,
U8_3BYTE_MAX = 0xFFFF,
U8_4BYTE_MIN = 0x10000,
U8_4BYTE_MAX = UNICODE_MAX,
U16_1WORD_MAX = 0xFFFF,
U16_2WORD_MIN = 0x10000,
U16_2WORD_MAX = UNICODE_MAX,
};
template <int Len>
void putCpNeT (wchar_t*& p, unsigned long aCp);
/// Puts a code-point in wchar_t encoding, w/o error-checking
/// @param [in,out] p position to put to
/// @param [in] aCp code-point, surely valid
inline void putCpNe (wchar_t*& p, unsigned long aCp)
{ putCpNeT<sizeof(wchar_t)>(p, aCp); }
} // namespace str
template <>
void str::putCpNeT<2> (wchar_t*& p, unsigned long aCp)
{
if (aCp < U16_2WORD_MIN) { // 1 word
*(p++) = static_cast<wchar_t>(aCp);
} else if (aCp <= U16_2WORD_MAX) { // 2 words
aCp -= U16_2WORD_MIN;
// Hi word
const wchar_t lo10 = aCp & 0x3FF;
const wchar_t hi10 = aCp >> 10;
*(p++) = static_cast<wchar_t>(0xD800 | hi10);
*(p++) = static_cast<wchar_t>(0xDC00 | lo10);
}
}
void str::appendCp(std::wstring& s, unsigned long aCp)
{
wchar_t c[5];
wchar_t* end = c;
putCpNe(end, aCp);
s.append(c, end);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question