阿里兄 发表于 2022-11-26 12:01:11

C/C++生成unicode范围的字符

C/C++生成unicode范围的字符
看到有人问怎么生成unicode范围的字符,网上没有找到相关在线转换的网站,自己实现的一个也不难。


思路:
1、如果会上位机如QT,那会很简单,直接QString str;str.append( QChar(unicode) ),最后直接显示字符串即可;
2、纯C/C++代码生成,最简单就是使用uncode转成utf8字符串,再写文件就完成了。


附上纯C/C++的代码和运行exe。
#include <iostream>

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

//unicode转utf8
int enc_unicode_to_utf8_one(unsigned long unic, unsigned char *pOutput)
{
    //assert(pOutput != NULL);
    //assert(outSize >= 6);

    if ( unic <= 0x0000007F )
    {
      // * U-00000000 - U-0000007F:0xxxxxxx
      *pOutput   = (unic & 0x7F);
      return 1;
    }
    else if ( unic >= 0x00000080 && unic <= 0x000007FF )
    {
      // * U-00000080 - U-000007FF:110xxxxx 10xxxxxx
      *(pOutput+1) = (unic & 0x3F) | 0x80;
      *pOutput   = ((unic >> 6) & 0x1F) | 0xC0;
      return 2;
    }
    else if ( unic >= 0x00000800 && unic <= 0x0000FFFF )
    {
      // * U-00000800 - U-0000FFFF:1110xxxx 10xxxxxx 10xxxxxx
      *(pOutput+2) = (unic & 0x3F) | 0x80;
      *(pOutput+1) = ((unic >>6) & 0x3F) | 0x80;
      *pOutput   = ((unic >> 12) & 0x0F) | 0xE0;
      return 3;
    }
    else if ( unic >= 0x00010000 && unic <= 0x001FFFFF )
    {
      // * U-00010000 - U-001FFFFF:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
      *(pOutput+3) = (unic & 0x3F) | 0x80;
      *(pOutput+2) = ((unic >>6) & 0x3F) | 0x80;
      *(pOutput+1) = ((unic >> 12) & 0x3F) | 0x80;
      *pOutput   = ((unic >> 18) & 0x07) | 0xF0;
      return 4;
    }
    else if ( unic >= 0x00200000 && unic <= 0x03FFFFFF )
    {
      // * U-00200000 - U-03FFFFFF:111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
      *(pOutput+4) = (unic & 0x3F) | 0x80;
      *(pOutput+3) = ((unic >>6) & 0x3F) | 0x80;
      *(pOutput+2) = ((unic >> 12) & 0x3F) | 0x80;
      *(pOutput+1) = ((unic >> 18) & 0x3F) | 0x80;
      *pOutput   = ((unic >> 24) & 0x03) | 0xF8;
      return 5;
    }
    else if ( unic >= 0x04000000 && unic <= 0x7FFFFFFF )
    {
      // * U-04000000 - U-7FFFFFFF:1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
      *(pOutput+5) = (unic & 0x3F) | 0x80;
      *(pOutput+4) = ((unic >>6) & 0x3F) | 0x80;
      *(pOutput+3) = ((unic >> 12) & 0x3F) | 0x80;
      *(pOutput+2) = ((unic >> 18) & 0x3F) | 0x80;
      *(pOutput+1) = ((unic >> 24) & 0x3F) | 0x80;
      *pOutput   = ((unic >> 30) & 0x01) | 0xFC;
      return 6;
    }

    return 0;
}

int main(void)
{
    uint16_t unicode_min;//unicode最小值
    uint16_t unicode_max;//unicode最大值

    //下面获取输入与输出
    printf("input unicode min (like:4f60):");
    cin >> hex >> unicode_min ;
    printf("input unicode max (like:4f61):");
    cin >> hex >> unicode_max ;

    int str_len = unicode_max-unicode_min+1;//计算字符串
    int mem_len = str_len*6;//所需最大内存
    uint8_t *mem = (uint8_t *)malloc(mem_len);
    memset(mem, 0, mem_len);
    uint8_t *p = (uint8_t *)mem;
    int string_bytes = 0;//统计最大字节数
    int len;//每个unicode转为utf8所占的字节数
    for (uint16_t unicode = unicode_min; unicode <= unicode_max; ++unicode) {
      len = enc_unicode_to_utf8_one(unicode, p);
      p += len;
      string_bytes += len;
    }

    char file_name;
    sprintf(file_name, "0x%04x-0x%04x.txt", unicode_min, unicode_max);//文件名

    FILE* file = fopen(file_name,"w"); //打开文件
    fwrite(mem, 1, string_bytes, file); //写入操作
    fclose(file);//关闭文件

    free(mem);//最后释放内存
    system("pause");
    return 0;
}




页: [1]
查看完整版本: C/C++生成unicode范围的字符