编译: g++ -o x x.cpp -licuuc -licui18n
请大家确认是否安装icu库
#include <stdio.h>
#include <string.h>
#include <unicode/ucnv.h>
#include <unicode/utypes.h>
#include <unicode/ucsdet.h>
#define BUF_MAX 4096
/*
* data, 传入参数, 需要探测的字符串
* len, 传入参数, 探测字符串长度
* detected 传出参数, 探测的最有可能的字符编码名称, 调用者需要释放该字段
**/
bool detectTextEncoding(const char *data, int32_t len, char **detected)
{
UCharsetDetector* csd;
const UCharsetMatch **csm;
int32_t match, matchCount = 0;
UErrorCode status = U_ZERO_ERROR;
csd = ucsdet_open(&status);
if(status != U_ZERO_ERROR)
return false;
ucsdet_setText(csd, data, len, &status);
if(status != U_ZERO_ERROR)
return false;
csm = ucsdet_d