简体   繁体   中英

conversion from std::vector<char> to wchar_t*

i'm trying to read ID3 frames and their values with TagLib (1) and index them with CLucene (2). the former returns frame ID's as std::vector<char> (3) and the latter writes field names as tchar* [ wchar_t* in Linux] (4). i need to make a link between the two. how can i convert from std::vector<char> to wchar_t* by means of the STL? thank you

(1) http://developer.kde.org/~wheeler/taglib.html
(2) http://clucene.sourceforge.net/
(3) http://developer.kde.org/~wheeler/taglib/api/classTagLib_1_1ID3v2_1_1Frame.html#6aac53ec5893fd15164cd22c6bdb5dfd
(4) http://ohnopublishing.net/doc/clucene-0.9.21b/html/classlucene_1_1document_1_1Field.html#59b0082e2ade8c78a51a64fe99e684b2

In a simple case where your char s don't contain any accented characters or anything like that, you can just copy each one to the destination and use it:

std::vector<char> frameID;

std::vector<wchar_t> field_name;

std::copy(frameID.begin(), frameID.end(), std::back_inserter(field_name));

lucene_write_field(&field_name[0], field_name.length());

My guess is that for ID3 frame ID's you don't have accented characters and such, so that'll probably be all you need. If you do have a possibility of accented characters and such, things get more complex in a hurry -- you'll need to convert from something like ISO 8859-x to (probably) UTF-16 Unicode. To do that, you need a code-page that tells you how to interpret the input (ie, there are several varieties of ISO 8859, and one for French input will be different from one for Russian, for example).

In order to prevent large char values from becoming negative wchar_t values you need to make sure that you cast to unsigned. This works though I believe it's technically undefined:

unsigned char* uchar = reinterpret_cast<unsigned char*>(&vect[0]);

std::vector<wchar_t> vwchar(uchar, uchar + vect.size());

This is important if your text contains anything above 127 in the character set.

Also keep in mind that none of these answer correctly deal with UTF-anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM