里飞网

 找回密码
 立即注册
查看: 1026|回复: 0
打印 上一主题 下一主题

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

[复制链接]

45

主题

290

帖子

2492

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2492
跳转到指定楼层
楼主
发表于 2022-11-26 12:01:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
C/C++生成unicode范围的字符
看到有人问怎么生成unicode范围的字符,网上没有找到相关在线转换的网站,自己实现的一个也不难。


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


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

  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. using namespace std;

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

  12.     if ( unic <= 0x0000007F )
  13.     {
  14.         // * U-00000000 - U-0000007F:  0xxxxxxx
  15.         *pOutput     = (unic & 0x7F);
  16.         return 1;
  17.     }
  18.     else if ( unic >= 0x00000080 && unic <= 0x000007FF )
  19.     {
  20.         // * U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
  21.         *(pOutput+1) = (unic & 0x3F) | 0x80;
  22.         *pOutput     = ((unic >> 6) & 0x1F) | 0xC0;
  23.         return 2;
  24.     }
  25.     else if ( unic >= 0x00000800 && unic <= 0x0000FFFF )
  26.     {
  27.         // * U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
  28.         *(pOutput+2) = (unic & 0x3F) | 0x80;
  29.         *(pOutput+1) = ((unic >>  6) & 0x3F) | 0x80;
  30.         *pOutput     = ((unic >> 12) & 0x0F) | 0xE0;
  31.         return 3;
  32.     }
  33.     else if ( unic >= 0x00010000 && unic <= 0x001FFFFF )
  34.     {
  35.         // * U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  36.         *(pOutput+3) = (unic & 0x3F) | 0x80;
  37.         *(pOutput+2) = ((unic >>  6) & 0x3F) | 0x80;
  38.         *(pOutput+1) = ((unic >> 12) & 0x3F) | 0x80;
  39.         *pOutput     = ((unic >> 18) & 0x07) | 0xF0;
  40.         return 4;
  41.     }
  42.     else if ( unic >= 0x00200000 && unic <= 0x03FFFFFF )
  43.     {
  44.         // * U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  45.         *(pOutput+4) = (unic & 0x3F) | 0x80;
  46.         *(pOutput+3) = ((unic >>  6) & 0x3F) | 0x80;
  47.         *(pOutput+2) = ((unic >> 12) & 0x3F) | 0x80;
  48.         *(pOutput+1) = ((unic >> 18) & 0x3F) | 0x80;
  49.         *pOutput     = ((unic >> 24) & 0x03) | 0xF8;
  50.         return 5;
  51.     }
  52.     else if ( unic >= 0x04000000 && unic <= 0x7FFFFFFF )
  53.     {
  54.         // * U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  55.         *(pOutput+5) = (unic & 0x3F) | 0x80;
  56.         *(pOutput+4) = ((unic >>  6) & 0x3F) | 0x80;
  57.         *(pOutput+3) = ((unic >> 12) & 0x3F) | 0x80;
  58.         *(pOutput+2) = ((unic >> 18) & 0x3F) | 0x80;
  59.         *(pOutput+1) = ((unic >> 24) & 0x3F) | 0x80;
  60.         *pOutput     = ((unic >> 30) & 0x01) | 0xFC;
  61.         return 6;
  62.     }

  63.     return 0;
  64. }

  65. int main(void)
  66. {
  67.     uint16_t unicode_min;//unicode最小值
  68.     uint16_t unicode_max;//unicode最大值

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

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

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

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

  91.     free(mem);//最后释放内存
  92.     system("pause");
  93.     return 0;
  94. }
复制代码



生成unicode范围的字符.zip (397.9 KB, 下载次数: 345)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|里飞网  

GMT+8, 2024-4-24 19:23 , Processed in 0.092049 second(s), 5 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表