用C/C++库函数怎样实现关于GBK-2312和UTF8之间的字符串相互转化,如果有源码更好,提供相应库函数也非常感谢。
5 个解决方案
#1
GNU libiconv 编码转换库
#2
参考一下吧
#include <iconv.h>
#include <errno.h>
void Encoding::Open() {
cd_ = iconv_open(to_charset_, from_charset_); //from_charset_="GBK" to_charset_="UTF-8"
if (cd_ == (iconv_t) (-1)) {
printf(
"The conversion from %s to %s is not supported by the implementation.",
from_charset_, to_charset_);
cd_ = NULL;
}
}
int Encoding::ConvertCharset(char* inBuf, int inBufLen, char* outBuf,
int outBufLen) {
if (cd_ == NULL) {
printf("Please init iconv first.\n");
return -1;
}
if(inBuf == NULL || inBufLen == 0)
return 0;
if(outBuf == NULL || outBufLen == 0)
return 0;
char* tmpbuff = outBuf;
int tmplen = outBufLen;
size_t size = iconv(cd_, &inBuf, (size_t *) &inBufLen, &tmpbuff,
(size_t *) &tmplen);
if (size == (size_t) (-1)) {
if (errno == EILSEQ) {
printf(
"An invalid multibyte sequence has been encountered in the input.");
} else if (errno == E2BIG) {
printf("There is not sufficient room at *outbuf.");
} else if (errno == EINVAL) {
printf(
"An incomplete multibyte sequence has been encountered in the input.");
}
return -1;
}
return outBufLen - tmplen - 1;
}
#include <iconv.h>
#include <errno.h>
void Encoding::Open() {
cd_ = iconv_open(to_charset_, from_charset_); //from_charset_="GBK" to_charset_="UTF-8"
if (cd_ == (iconv_t) (-1)) {
printf(
"The conversion from %s to %s is not supported by the implementation.",
from_charset_, to_charset_);
cd_ = NULL;
}
}
int Encoding::ConvertCharset(char* inBuf, int inBufLen, char* outBuf,
int outBufLen) {
if (cd_ == NULL) {
printf("Please init iconv first.\n");
return -1;
}
if(inBuf == NULL || inBufLen == 0)
return 0;
if(outBuf == NULL || outBufLen == 0)
return 0;
char* tmpbuff = outBuf;
int tmplen = outBufLen;
size_t size = iconv(cd_, &inBuf, (size_t *) &inBufLen, &tmpbuff,
(size_t *) &tmplen);
if (size == (size_t) (-1)) {
if (errno == EILSEQ) {
printf(
"An invalid multibyte sequence has been encountered in the input.");
} else if (errno == E2BIG) {
printf("There is not sufficient room at *outbuf.");
} else if (errno == EINVAL) {
printf(
"An incomplete multibyte sequence has been encountered in the input.");
}
return -1;
}
return outBufLen - tmplen - 1;
}
#4
自己google下。
另外听说过gbk和gb2312,GBK-2312这种叫法就很不正经。
另外听说过gbk和gb2312,GBK-2312这种叫法就很不正经。
#5
gbk<--->unicode<---->utf8
#1
GNU libiconv 编码转换库
#2
参考一下吧
#include <iconv.h>
#include <errno.h>
void Encoding::Open() {
cd_ = iconv_open(to_charset_, from_charset_); //from_charset_="GBK" to_charset_="UTF-8"
if (cd_ == (iconv_t) (-1)) {
printf(
"The conversion from %s to %s is not supported by the implementation.",
from_charset_, to_charset_);
cd_ = NULL;
}
}
int Encoding::ConvertCharset(char* inBuf, int inBufLen, char* outBuf,
int outBufLen) {
if (cd_ == NULL) {
printf("Please init iconv first.\n");
return -1;
}
if(inBuf == NULL || inBufLen == 0)
return 0;
if(outBuf == NULL || outBufLen == 0)
return 0;
char* tmpbuff = outBuf;
int tmplen = outBufLen;
size_t size = iconv(cd_, &inBuf, (size_t *) &inBufLen, &tmpbuff,
(size_t *) &tmplen);
if (size == (size_t) (-1)) {
if (errno == EILSEQ) {
printf(
"An invalid multibyte sequence has been encountered in the input.");
} else if (errno == E2BIG) {
printf("There is not sufficient room at *outbuf.");
} else if (errno == EINVAL) {
printf(
"An incomplete multibyte sequence has been encountered in the input.");
}
return -1;
}
return outBufLen - tmplen - 1;
}
#include <iconv.h>
#include <errno.h>
void Encoding::Open() {
cd_ = iconv_open(to_charset_, from_charset_); //from_charset_="GBK" to_charset_="UTF-8"
if (cd_ == (iconv_t) (-1)) {
printf(
"The conversion from %s to %s is not supported by the implementation.",
from_charset_, to_charset_);
cd_ = NULL;
}
}
int Encoding::ConvertCharset(char* inBuf, int inBufLen, char* outBuf,
int outBufLen) {
if (cd_ == NULL) {
printf("Please init iconv first.\n");
return -1;
}
if(inBuf == NULL || inBufLen == 0)
return 0;
if(outBuf == NULL || outBufLen == 0)
return 0;
char* tmpbuff = outBuf;
int tmplen = outBufLen;
size_t size = iconv(cd_, &inBuf, (size_t *) &inBufLen, &tmpbuff,
(size_t *) &tmplen);
if (size == (size_t) (-1)) {
if (errno == EILSEQ) {
printf(
"An invalid multibyte sequence has been encountered in the input.");
} else if (errno == E2BIG) {
printf("There is not sufficient room at *outbuf.");
} else if (errno == EINVAL) {
printf(
"An incomplete multibyte sequence has been encountered in the input.");
}
return -1;
}
return outBufLen - tmplen - 1;
}
#3
#4
自己google下。
另外听说过gbk和gb2312,GBK-2312这种叫法就很不正经。
另外听说过gbk和gb2312,GBK-2312这种叫法就很不正经。
#5
gbk<--->unicode<---->utf8