首页 > 其他分享 >用icu探测字符集

用icu探测字符集

时间:2023-12-13 15:12:56浏览次数:23  
标签:status ucsdet 字符集 探测 char csm match include icu

g++ str-charset.cpp -licui18n

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unicode/ucnv.h>
#include <unicode/utypes.h>
//#include <unicode/urename.h>
#include <unicode/ucsdet.h>

bool what_charset(const char *data, int len, char **detected)
{
UCharsetDetector* csd;
const UCharsetMatch **csm;
int 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_detectAll(csd, &matchCount, &status);
if(status != U_ZERO_ERROR)
return false;
#if 1 //打印出探测的可能的编码
for(match = 0; match < matchCount; match += 1)
{
const char *name = ucsdet_getName(csm[match], &status);
const char *lang = ucsdet_getLanguage(csm[match], &status);
int confidence = ucsdet_getConfidence(csm[match], &status);
if (lang == NULL || strlen(lang) == 0)
lang = "**";
printf("%s (%s) %d\n", name, lang, confidence);
}
#endif
if(matchCount > 0)
{
*detected = strdup(ucsdet_getName(csm[0], &status)); //分配了内
if(status != U_ZERO_ERROR)
return false;
}

printf("charset = %s n", *detected);
ucsdet_close(csd);

return true;
}

int main(int argc, char* argv[])
{
std::string data = "This is a 测试用数据";
char buf[128];
char *str[1];
str[0] = buf;

what_charset((const char *)data.c_str(), data.length(), str);

return 0;
}

标签:status,ucsdet,字符集,探测,char,csm,match,include,icu
From: https://www.cnblogs.com/yyybill/p/17899036.html

相关文章

  • 火星探测器背后的人工智能:从原理到实战的强化学习
    本文详细探讨了强化学习在火星探测器任务中的应用。从基础概念到模型设计,再到实战代码演示,我们深入分析了任务需求、环境模型构建及算法实现,提供了一个全面的强化学习案例解析,旨在推动人工智能技术在太空探索中的应用。关注TechLead,分享AI全维度知识。作者拥有10+年互联网服务......
  • 1804:错误探测
    原题这道题虽然一次ac了,而且写的很顺,没调就过了,所以记录一下想法撒#include<bits/stdc++.h>usingnamespacestd;intmain(){ints[101][101]={};intx[101],y[101];intn,flag=0,x1,sumx=0,y1,sumy=0;memset(x,0,sizeof(x));memset(y,0,s......
  • Redis生产实战-Redis集群故障探测以及降级方案设计
    Redis集群故障探测在生产环境中,如果Redis集群崩溃了,那么会导致大量的请求打到数据库中,会导致整个系统都崩溃,所以系统需要可以识别缓存故障,限流保护数据库,并且启动接口的降级机制降级方案设计我们在系统中操作Redis一般都是通过工具类来进行操作的,假设工具类有两个RedisCache......
  • 《安富莱嵌入式周报》第328期:自主微型机器人,火星探测器发射前失误故障分析,微软推出12
    周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 更新一期视频教程:【实战技能】单步运行源码分析,一期视频整明白FreeRTOS内核源码框架和运行机制,RTOSTrace链表功能展示https://www.armbbs.cn/forum.php?mod=viewthread&tid......
  • Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) MySql字符集及校对规则不
    查询时报错--用于解决报错>1267-Illegalmixofcollations(utf8mb4_0900_ai_ci,IMPLICIT)and(utf8mb4_general_ci,IMPLICIT)foroperation'='这个错误是由于在进行比较操作时涉及到不同的字符集和校对规则(collations)。涉及到了utf8mb4_0900_ai_ci和utf8mb4_general......
  • 11.30二次探测法解决冲突
    设哈希表长为14,哈希函数是H=key%11,表中已有数据的关键字为15,38,61,84共四个,现要将关键字为49的元素加到表中,用二次探测法解决冲突,则放入的位置是(9)。15的位置是4,38的位置是5,61的位置是6,84的位置为749对应5和38冲突所以要用二次探索就是跳跃式的加数直到不重复且不超过哈希表长{1......
  • 设置 IntelliJ IDEA 的默认字符集的方法
    设置IntelliJIDEA的默认字符集的方法 1、在顶部菜单栏找到“帮助(Help)”项(通常是最后一项),点击它并在弹出的二级菜单中选择“编辑定制虚拟机选项(EditCustomVMOptions...)”项。2、在默认追加如下配置:-Dfile.encoding=UTF-8 3、保存并重启IntelliJIDEA即可。......
  • 【文档翻译】每个开发者都必须了解的关于Unicode和字符集的基本知识
    本文档译自joelonsoftware.com的文章"TheAbsoluteMinimumEverySoftwareDeveloperAbsolutely,PositivelyMustKnowAboutUnicodeandCharacterSets(NoExcuses!)",作者joel,原文参见此处概述-Overview你是否在某一个平凡的日子,思考过那个神秘的Content-Type标......
  • 程序字符集知识
    一、基础知识:为什么采用中文编程不行?https://mp.weixin.qq.com/s/v22aUQ-9ietJYdz0wMNUUQ字符编码那点事:快速理解ASCII、Unicode、GBK和UTF-8https://zhuanlan.zhihu.com/p/38333902主要(分清)认识两个概念:字符集、字符编码(规则):字符集(Charset):是一个系统支持的所有抽象字符......
  • 如何修改Azure SQL server数据库的字符集(UTF-8)排序规则
    ......