Josh Posted June 17, 2016 Share Posted June 17, 2016 Does anyone know how to convert a wchar_t* into an xchar2b* string data type? Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
IgorBgz90 Posted June 17, 2016 Share Posted June 17, 2016 Hi Josh! Maybe this will help: Code from here: http://xopendisplay.hilltopia.ca/2009/Mar/ int utf8toXChar2b(XChar2b *output_r, int outsize, const char *input, int inlen){ int j, k; for(j =0, k=0; j < inlen && k < outsize; j ++){ unsigned char c = input[j]; if (c < 128) { output_r[k].byte1 = 0; output_r[k].byte2 = c; k++; } else if (c < 0xC0) { /* we're inside a character we don't know */ continue; } else switch(c&0xF0){ case 0xC0: case 0xD0: /* two bytes 5+6 = 11 bits */ if (inlen < j+1){ return k; } output_r[k].byte1 = (c&0x1C) >> 2; j++; output_r[k].byte2 = ((c&0x3) << 6) + (input[j]&0x3F); k++; break; case 0xE0: /* three bytes 4+6+6 = 16 bits */ if (inlen < j+2){ return k; } j++; output_r[k].byte1 = ((c&0xF) << 4) + ((input[j]&0x3C) >> 2); c = input[j]; j++; output_r[k].byte2 = ((c&0x3) << 6) + (input[j]&0x3F); k++; break; case 0xFF: /* the character uses more than 16 bits */ continue; } } return k; } And convert char* to wchar_t wchar_t * filename= L"C:\\test"; char* c = (char*)filename; Quote Link to comment Share on other sites More sharing options...
Josh Posted June 17, 2016 Author Share Posted June 17, 2016 This is converting a char string, not a wchar. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Bolt Posted June 17, 2016 Share Posted June 17, 2016 You can still use it if you convert wchar_t to UTF8 first, then to XChar2b. Note that the size of wchar_t is different on Windows/Linux OSes. 2 bytes for Windows, 4 bytes for Linux. wchar_t is not portable friendly, which is why most people use UTF8. Quote Ultimate Unwrap 3D: http://www.unwrap3d.com Link to comment Share on other sites More sharing options...
Josh Posted June 17, 2016 Author Share Posted June 17, 2016 UTF8 only allows 256 characters, right? I'm writing a new library and trying to support Cyrillic and other character sets. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
martyj Posted June 17, 2016 Share Posted June 17, 2016 Check out this. http://en.cppreference.com/w/cpp/locale/codecvt Quote Link to comment Share on other sites More sharing options...
Bolt Posted June 17, 2016 Share Posted June 17, 2016 UTF8 is capable of encoding all possible characters defined by Unicode, which is over one million characters. https://en.wikipedia.org/wiki/UTF-8 If you just use UTF8 for ASCII text (0 to 127), it looks exactly the same. It uses a single byte for a single character. But, higher up, it uses 2 or 3 bytes per character. 1 Quote Ultimate Unwrap 3D: http://www.unwrap3d.com Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.