c++ - How to convert wstring to string as escapes -


I have a wstring, what is the best way to save it, such as \ u043d \ u043e \ U043c \ U0430 ?

Below does one work but does not look best:

  string output; For (wchar_t chr: wtst) {four codes [7]; Sprintf (code, "\\ u% 0.4x", chr); Output + = code; } A less compact but faster version that a) Allocated prematurely and b)    

  std :: wstring wstr (L "\ x043d \ x043e \ x043c \ x0430") ; Std :: string sstr; // reserve memory 1 hit to avoid copying for longer string; static size_t const nchars_per_code = 6; Sstr.reserve (wstr.size () * nchars_per_code); Four code [nchars_per_code]; Code [0] = '\\'; Code [1] = 'U'; Static Four Constants * Constant Hexelt = "0123456789abcdef"; Std :: wstring :: const_iterator i = wstr.begin (); Std :: wstring :: const_iterator e = wstr.end (); For (; i! = E; ++I) {unsigned wc = * i; Code [2] = (Hexlat [(wc> & gt; 12) & amp; 0xF]); Code [3] = (Hexlat [(wc & 8; & amp; 8; & amp; 0xF]); Code [4] = (Hexlat [(wc> 4; & amp; 0xF]); Code [5] = (Hexalit [(WC) and 0xF]); Sstr.append (code, code + nchars_per_code);}    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -