utf8 to unicode

C语言实现Unicode编码和UTF-8互转 在C语言中,我们需要进行Unicode编码和UTF-8编码的转换,本篇文章将介绍如何实现这两个编码之间的相互转换。UTF-8编码使用1~4个字节来表示不同的字符,每一个字节都以“0”开头,其中第一个字节还包含了该字符所需的字节数信息。UTF-8编码使用1~4个字节来表示不同的字符,每一个字节都以“0”开头,其中第一个字节还包含了该字符所需的字节数信息。最后,我们将解析出的Unicode编码放入wchar_t类型的数组中,并且在数组末尾添加一个L’\0’字符,以表示数组的结束。 阅读详情

#include <stdio.h>

#include <string.h>

#include <malloc.h>

#include <memory.h>

 

#ifdef WIN32

#define uint8_t  unsigned __int8

#define uint16_t unsigned __int16

#define uint32_t unsigned __int32

#define uint64_t unsigned __int64

 

#define int8_t  __int8

#define int16_t __int16

#define int32_t __int32

#endif

 

int unicode_to_utf8(uint16_t *in, int insize, uint8_t **out)

{

    int i = 0;

    int outsize = 0;

    int charscount = 0;

    uint8_t *result = NULL;

    uint8_t *tmp = NULL;

 

    charscount = insize / sizeof(uint16_t);

    result = (uint8_t *)malloc(charscount * 3 + 1);

    memset(result, 0, charscount * 3 + 1);

    tmp = result;

 

    for (i = 0; i < charscount; i++)

    {

        uint16_t unicode = in[i];

 

        if (unicode >= 0x0000 && unicode <= 0x007f)

        {

            *tmp = (uint8_t)unicode;

            tmp += 1;

            outsize += 1;

        }

        else if (unicode >= 0x0080 && unicode <= 0x07ff)

        {

            *tmp = 0xc0 | (unicode >> 6);

            tmp += 1;

            *tmp = 0x80 | (unicode & (0xff >> 2));

            tmp += 1;

            outsize += 2;

        }

        else if (unicode >= 0x0800 && unicode <= 0xffff)

        {

            *tmp = 0xe0 | (unicode >> 12);

            tmp += 1;

            *tmp = 0x80 | (unicode >> 6 & 0x00ff);

            tmp += 1;

            *tmp = 0x80 | (unicode & (0xff >> 2));

            tmp += 1;

            outsize += 3;

        }

 

    }

 

    *tmp = '/0';

    *out = result;

    return 0;

}

 

int utf8_to_unicode(uint8_t *in, uint16_t **out, int *outsize)

{

    uint8_t *p = in;

    uint16_t *result = NULL;

    int resultsize = 0;

    uint8_t *tmp = NULL;

 

    result = (uint16_t *)malloc(strlen(in) * 2 + 2); /* should be enough */

    memset(result, 0, strlen(in) * 2 + 2);

    tmp = (uint8_t *)result;

 

    while(*p)

    {

        if (*p >= 0x00 && *p <= 0x7f)

        {

            *tmp = *p;

            tmp++;

            *tmp = '/0';

            resultsize += 2;

        }

        else if ((*p & (0xff << 5))== 0xc0)

        {

            uint16_t t = 0;

            uint8_t t1 = 0;

            uint8_t t2 = 0;

 

            t1 = *p & (0xff >> 3);

            p++;

            t2 = *p & (0xff >> 2);

 

            *tmp = t2 | ((t1 & (0xff >> 6)) << 6);//t1 >> 2;

            tmp++;

 

            *tmp = t1 >> 2;//t2 | ((t1 & (0xff >> 6)) << 6);

            tmp++;

 

            resultsize += 2;

        }

        else if ((*p & (0xff << 4))== 0xe0)

        {

            uint16_t t = 0;

            uint8_t t1 = 0;

            uint8_t t2 = 0;

            uint8_t t3 = 0;

 

            t1 = *p & (0xff >> 3);

            p++;

            t2 = *p & (0xff >> 2);

            p++;

            t3 = *p & (0xff >> 2);

 

            //Little Endian

            *tmp = ((t2 & (0xff >> 6)) << 6) | t3;//(t1 << 4) | (t2 >> 2);

            tmp++;

 

            *tmp = (t1 << 4) | (t2 >> 2);//((t2 & (0xff >> 6)) << 6) | t3;

            tmp++;

            resultsize += 2;

        }

 

        p++;

    }

 

    *tmp = '/0';

    tmp++;

    *tmp = '/0';

    resultsize += 2;

 

    *out = result;

    *outsize = resultsize; 

    return 0;

}

 

void dump_utf8(uint8_t *utf8)

{

    uint8_t *p = utf8;

 

    while(*p)

    {

        printf("%02X", *p);

        p++;

    }

    putchar('/n');

}

 

void dump_unicode(uint16_t *utf16, int size)

{

    uint8_t *p = (uint8_t *)utf16;

    int i = 0;

 

    for (i = 0; i < size; i++)

    {

        printf("%02X", *p);

        p++;

    }

    putchar('/n');

}

 

int main()

{

    uint16_t unicode[] = L"中文";

    uint8_t *utf8 = NULL;

 

    int unisize = 0;

    uint16_t *uni = NULL;

 

    printf("original unicode: /n");

    dump_unicode(unicode, sizeof(unicode));

 

    printf("converted to utf8: /n");

    unicode_to_utf8(unicode, sizeof(unicode), &utf8);

    dump_utf8(utf8);

 

    printf("converted to unicode: /n");

    utf8_to_unicode(utf8, &uni, &unisize);

    dump_unicode(uni, unisize);

 

    free(utf8);

    free(uni);

 

    return 0;

}

C语言实现Utf8转Unicode(附完整源码) 阅读详情

相关推荐

C语言实现Utf8转Unicode

在上面的代码中,我们定义了一个包含UTF-8编码的字符串utf8_str,然后依次处理每个字符,将其转换为对应的Unicode字符集中的字符。具体操作是,首先判断该字符需要使用几个字节进行编码(即判断它的前几位是0、110、1110还是11110),然后根据不同的字节数,使用位运算将其转换为对应的Unicode字符集中的字符。在程序开发中,需要将UTF-8编码的字符转换为Unicode字符集中的相应字符,这就需要进行UTF-8到Unicode的转换。了解完UTF-8编码的原理后,我们就可以开始编码了。

HackQuestR的博客 1331

UTF8转Unicode

int GetUtf8ByteNumForWord(char firstCh) { int nRet=0; __asm { movzx ecx,byte ptr[firstCh] and ecx,0xE0 jz done test ecx,0x80 jnz lbm m...

冷月醉雪的博客 613

SNAP新手避坑指南:手把手教你搞定Sentinel-2影像的镶嵌与重采样(附详细参数)

本文为SNAP新手提供Sentinel-2影像处理的详细指南,重点讲解镶嵌与重采样的关键步骤和参数设置。通过实际案例和常见问题解决方案,帮助用户避免常见错误,高效完成遥感影像处理任务。

weixin_29271263的博客 410

UTF8编码转Unicode编码(C语言)

/** * 功能:UTF8编码转Unicode编码 * 参数:dst 目标字符串,src 源字符串 */ WORD UTF8_to_Unicode(BYTE *dst, BYTE *src) {     WORD i = 0, unicode = 0, ii, iii;     int codeLen = 0;         while ( *src )     {

xywlzd的专栏 1876

ansys workbench 静力结构分析 高阶教程

ansys workbench 静力结构分析 高阶教程 目录ansys workbench 静力结构分析 高阶教程新建工程模型导入设置材料模型简化: 主要内容: 材料设置、模型简化、网格细化、约束分析、接触设置、求解设置、报错处理、结果分析 基础教程: ansys workbench 静力结构分析 基础操作流程(入门) ansys版本: 19.0(16.0及以上均可) 参考资料: 推荐教学视频《有限元分析 张晔》(超详细 ???? )、ansys workbench 超级学习手册(查阅用) 说明:一些基

weixin_43434624的博客 5万+

Unity中URP实现水体效果(泡沫)

在上篇文章中,我们实现了水体的深度。在这篇文章中,我们来使用上篇文章采样后的深度图来实现水中的泡沫效果。

楠溪泽岸的博客 3495

UTF8 to unicode

UTF8并不算是一种电脑编码,而是一种储存和传送的格式,如前所述,每个Unicode/UCS字符都以 2或4个bytes来储存,看看以下的比较: 以"I am Chinese"为例 用ANSI储存:             12 Bytes 用Unicode/UCS2储存:     24 Bytes + 2 Bytes(header) 用UCS4储存:             48 Byt

萌萌的It人 www.itmmd.com 1168

Fluent 中多相流模型全面解析

3.颗粒或液滴在连续相中的运动:若关注颗粒或液滴在连续相中的运动轨迹、分布规律以及与连续相的相互作用,如喷雾干燥、气力输送等过程,Eulerian-Lagrangian 模型则是最佳选择。Eulerian-Eulerian 模型可以准确模拟气体在固体颗粒中的流动情况,包括颗粒的流化状态、气体与颗粒之间的传热传质过程,为气固流化床的优化设计和操作提供依据。通过该模型,可以了解混合器内不同位置的混合程度、颗粒的运动轨迹以及液固相间的相互作用,从而改进混合器的结构和操作条件,提高混合效率。

khhdp的博客 3098

unicode to utf8 php,php怎样实现utf8转unicode?

php实现utf8转unicode的方法:首先将第一个字节的0100提取出来;然后将得到的结果左移12位;接着提取111101,将所得到的结果左移6位与最高字节所得的结果取或;最后以此类推第n位直接与111111【0x3F】取与即可。php实现utf8转unicode的方法:当然在UTF-8到Unicode的转换也是通过移位等来完成的,就是把UTF-8那些格式相应的位置的二进制数给揪出来。在例子中...

weixin_39771351的博客 126

SAP-ABAP:在SAP系统中,COEP表(成本控制对象行项目表)详解

在SAP系统中,COEP表(成本控制对象行项目表)详解

baidu_35680696的博客 1844

153页PPT华为市场管理全景:战略、逻辑、阶段与工具深度解析

该文档围绕华为市场管理(MM)方法论展开,深入探讨了战略规划相关内容,涵盖战略核心命题、形成方式、分析工具,以及 MM 方法论的具体流程、各阶段任务和数据规划,旨在帮助企业制定科学合理的市场战略。MM 方法论框架在通信、家电、医疗设备等行业均有应用,通过理解市场、市场细分、组合分析等环节,企业能够精准定位市场,优化资源配置,提升市场竞争力,实现持续发展。MM 方法论框架由市场定义、能力评估和投资优先级排序 3 个基本要素组成,通过多个关键步骤,帮助企业全面了解市场,制定并执行有效的业务战略和规划。

cdfunlove的博客 1227

utf8_to_unicode in PHP

想使用8:UCS2格式编码下行短信的时候,需要把UTF8转换成Unicode的十六进制编码,但是PHP没有内置Unicode的支持,怎么办呢? /***从UTF8转换成unicodebeta1.0*@parammixed$string要转换的字符串,*@returnunicode的十六进制编码*/functionutf8_to_unicode_hex($string){...

weixin_30702887的博客 88

阿里云 Docker 镜像加速收费后:这些免费替代方案值得收藏

阿里云Docker镜像加速服务收费后,国内开发者急需免费替代方案。本文推荐2025年仍可用的7个免费镜像源,包括轩辕镜像、DaoCloud、网易云等,并提供多源配置方案以提高稳定性。建议开发者在CI/CD环境中配置多个镜像源,避免构建失败。文章还提供了详细的配置步骤和使用建议,适合个人和团队开发者参考。

sg_knight的专栏 1942

unicode to utf8 转化

#include #include #include  //计算需要的char的长度int wstr2strLen(const wchar_t *pwstr){     int count = 0;     int i;     int len = wcslen(pwstr);     for(i = 0; i      {          unsigned short tmp =

daniel_yao的专栏 674

Open3D 计算FPFH特征【2025最新版】

FPFH特征描述子的python实现,本文最新更新时间为:2025年3月18日。

点云侠的博客 5286

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

UTF8 to Unicode

#include "stdafx.h"#include int _tmain(int argc, _TCHAR* argv[]){ char szUTF8 [] = {0xE4,0XBB,0XB7,0XE5,0X80,0XBC};//是汉语的价值两个字 int nLen = MultiByteToWideChar( CP_UTF8, 0, szUTF8, -1, NULL, NULL );//得到

tianyu的专栏 - Linux site:blog.csdn.net/wishfly 811

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

UTF8/ANSI to Unicode

std::wstring EM_UnicodeFileName(LPCSTR lpInFile){std::string strInFile = lpInFile;UINT CodePage = CP_ACP;//check if lpInFile is UT8, if is, remove the BOM and set the codepageif (strInFile.compare(0,

u012905879的专栏 366

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

unicode_to_utf8转换函数源码

unicode to utf8转换函数的源代码,适合所有操作系统,不需要ICONV库。linux 实现UNICODE到UTF8的转码函数,可以替代Windows里的MutiByteToWideChar 函数

白袍小将的博客 1513

unicode to utf8

https://www.jianshu.com/p/90ecc5987a18 转载于:https://blog.51cto.com/11114389/2402111

weixin_34368949的博客 129

php tounicode,utf8_to_unicode in PHP

想使用8:UCS2格式编码下行短信的时候,需要把UTF8转换成Unicode的十六进制编码,但是PHP没有内置Unicode的支持,怎么办呢?/* ** 从UTF8转换成unicode beta1.0* @param mixed $string要转换的字符串,* @return unicode的十六进制编码*/function utf8_to_unicode_hex( $string ){$le...

weixin_39901412的博客 132

unicode_to_utf8 in PHP

当短信格式为8:UCS2编码的时候,需要把Unicode的十六进制编码转换成UTF8,但是PHP没有内置Unicode的支持,怎么办呢? /***把Unicode的十六进制字符串转换成utf8的文本字符串*@parammixed$str要转换的字符串,不能为null*@returnutf8的文本字符串*/publicstaticfunctionunic...

weixin_30632899的博客 80

Utf8 char to Unicode/LocalCode TCHAR

/************************************************************************/ /* convert Utf8 char to Unicode/LocalCode TCHAR */ /**********************************************...

186

纯c 实现utf8与ucs2 相互转换

网上很多代码,好多都是单个字符转码,自己整理了一下分享一下,望指正 UCS2:2字节unicode utf8:多字节编码 int ucs2_to_utf8(const unsigned char *in, int ilen, unsigned char *out, int olen) {     int length = 0;     if(!out) return l

kailin_li的专栏 2514
上一篇: 产品经理和项目经理的区别
下一篇: Android的Binder机制浅析
vrix
博客等级 码龄24年 675粉丝 167原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址