icu 字符串编码探测及字符串编码转换实例

本文将介绍如何使用ICU库进行字符串编码的探测和转换。通过编译示例代码并确保已安装ICU库,我们可以实现不同编码间的字符串操作。
摘要由CSDN通过智能技术生成

编译: 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值