使用c语言实现utf16 转utf8

该代码实现了一个C语言函数,将UTF16编码的字符串转换为UTF8编码。函数处理了Unicode的代理对,并动态分配内存来存储转换后的UTF8字符串。在主函数中,它展示了如何使用这个转换函数并打印结果。
摘要由CSDN通过智能技术生成

#include <stdio.h>

#include <stdlib.h>



int utf16_to_utf8(const unsigned short* utf16_str, char** utf8_str) {

    int utf8_index = 0;

    int utf8_size = 1; // 初始化UTF8字符串大小为1,用于存储字符串结束'\0'的空间

    

    int i = 0;

    while (utf16_str[i] != '\0') {

        unsigned int unicode_code = utf16_str[i];

        

        if (unicode_code >= 0xD800 && unicode_code <= 0xDBFF && utf16_str[i+1] != '\0') {

            unsigned int surrogate_pair_code = utf16_str[i+1];

            if (surrogate_pair_code >= 0xDC00 && surrogate_pair_code <= 0xDFFF) {

                unicode_code = ((unicode_code - 0xD800) << 10) + (surrogate_pair_code - 0xDC00) + 0x10000;

                i += 1;

            }

        }

        

        if (unicode_code < 0x80) {

            utf8_size += 1;

        }

        else if (unicode_code < 0x800) {

            utf8_size += 2;

        }

        else if (unicode_code < 0x10000) {

            utf8_size += 3;

        }

        else {

            utf8_size += 4;

        }

        

        i += 1;

    }

    

    *utf8_str = (char*) malloc(utf8_size * sizeof(char));

    

    if (*utf8_str == NULL) {

        return 0; // 内存分配失败,返回错误码

    }

    

    i = 0;

    utf8_index = 0;

    while (utf16_str[i] != '\0') {

        unsigned int unicode_code = utf16_str[i];

        

        if (unicode_code >= 0xD800 && unicode_code <= 0xDBFF && utf16_str[i+1] != '\0') {

            unsigned int surrogate_pair_code = utf16_str[i+1];

            if (surrogate_pair_code >= 0xDC00 && surrogate_pair_code <= 0xDFFF) {

                unicode_code = ((unicode_code - 0xD800) << 10) + (surrogate_pair_code - 0xDC00) + 0x10000;

                i += 1;

            }

        }

        

        if (unicode_code < 0x80) {

            (*utf8_str)[utf8_index++] = unicode_code;

        }

        else if (unicode_code < 0x800) {

            (*utf8_str)[utf8_index++] = ((unicode_code >> 6) & 0x1F) | 0xC0;

            (*utf8_str)[utf8_index++] = (unicode_code & 0x3F) | 0x80;

        }

        else if (unicode_code < 0x10000) {

            (*utf8_str)[utf8_index++] = ((unicode_code >> 12) & 0x0F) | 0xE0;

            (*utf8_str)[utf8_index++] = ((unicode_code >> 6) & 0x3F) | 0x80;

            (*utf8_str)[utf8_index++] = (unicode_code & 0x3F) | 0x80;

        }

        else {

            (*utf8_str)[utf8_index++] = ((unicode_code >> 18) & 0x07) | 0xF0;

            (*utf8_str)[utf8_index++] = ((unicode_code >> 12) & 0x3F) | 0x80;

            (*utf8_str)[utf8_index++] = ((unicode_code >> 6) & 0x3F) | 0x80;

            (*utf8_str)[utf8_index++] = (unicode_code & 0x3F) | 0x80;

        }

        

        i += 1;

    }

    

    (*utf8_str)[utf8_index] = '\0';

    

    return 1; // 转换成功,返回成功码

}



int main() {

    unsigned short utf16_str[] = {0x4F60, 0x597D, 0xFF0C, 0x4E16, 0x754C, 0xFF01, '\0'}; // UTF16编码的字符串

    char* utf8_str = NULL;

    

    if (utf16_to_utf8(utf16_str, &utf8_str)) {

        printf("UTF8编码的字符串:%s\n", utf8_str);

        }

}

       

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UTF-8 是一种可变长度的编码方式,一个字符可能由 1 到 4 个字节组成,而 Unicode 是一种字符集,它规定了每个字符对应的码点,其中大部分字符使用 2 个字节表示,而一些罕见字符使用 4 个字节表示。 UTF-8 Unicode 的过程就是将 UTF-8 编码的字符换为它所对应的 Unicode 码点。下面是一个简单的 C 语言函数,用于将 UTF-8 字符串换为 Unicode 码点数组: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> int utf8_to_unicode(const char *utf8_str, int *unicode_arr, int max_len) { int i = 0, j = 0, len = strlen(utf8_str); while (i < len && j < max_len) { unsigned char c = utf8_str[i++]; if (c < 0x80) { // 1 byte character unicode_arr[j++] = c; } else if (c < 0xE0) { // 2 byte character unsigned char c2 = utf8_str[i++]; unicode_arr[j++] = ((c & 0x1F) << 6) | (c2 & 0x3F); } else if (c < 0xF0) { // 3 byte character unsigned char c2 = utf8_str[i++]; unsigned char c3 = utf8_str[i++]; unicode_arr[j++] = ((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F); } else { // 4 byte character unsigned char c2 = utf8_str[i++]; unsigned char c3 = utf8_str[i++]; unsigned char c4 = utf8_str[i++]; unicode_arr[j++] = ((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F); } } return j; } ``` 这个函数接受一个 UTF-8 编码的字符串,一个长度为 max_len 的整数数组,以及数组的最大长度。它会将 UTF-8 字符串换为对应的 Unicode 码点,并将结果存储在整数数组中,返回值为换后的码点数量。 下面是一个使用示例: ```c int main() { const char *utf8_str = "\xE6\x88\x91\xE7\x9A\x84\xE4\xB8\xAD\xE6\x96\x87\xE5\x90\x8D\xE5\xAD\x97\xE6\x98\xAF\xE5\x8D\xA1\xE7\x89\x8C\xE5\x85\xB1\xE4\xBA\xAB"; int unicode_arr[100]; int num_unicode = utf8_to_unicode(utf8_str, unicode_arr, 100); for (int i = 0; i < num_unicode; i++) { printf("%04X ", unicode_arr[i]); } printf("\n"); return 0; } ``` 这个示例输入一个 UTF-8 编码的字符串,并输出它所对应的 Unicode 码点。输出结果为: ``` 6211 7684 4E2D 6587 540D 5B57 662F 5361 724C 5171 4EAB ``` 可以看到,这个函数成功地将 UTF-8 编码的中文字符串换为了对应的 Unicode 码点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ls1300005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值