Created
June 25, 2019 06:43
-
-
Save chromabox/be9d5acfe1410c99e0eb3c6cb8c6401b to your computer and use it in GitHub Desktop.
iconvをC++で使う例(C++17必要)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool conv_iconv(const std::vector<uint8_t> *src,std::string &dest,const char* fromcode,const char* tocode) | |
{ | |
iconv_t ic = iconv_open(tocode,fromcode); | |
char *pin = (char*) src->data(); | |
size_t ilen = src->size(); | |
size_t blen = ilen*3+10; | |
size_t olen = blen; | |
size_t ret; | |
dest.resize(olen); | |
char *pou = (char*)dest.data(); | |
ret = iconv(ic,&pin,&ilen,&pou,&olen); | |
if(ret == (size_t)-1){ | |
// error | |
dest.clear(); | |
return false; | |
} | |
*pou = '\0'; | |
dest.resize(blen - olen); | |
iconv_close(ic); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment