这个问题不好回答,首先UTF-8编码只不过是一种Unicode的转换,兼容ASCII。
所以,UTF-8编码支持的最大字符编码应该是Unicode支持的最大字符编码。

理论上,UTF-8编码可以支持最大6字节:
00000000-0000007F 0xxxxxxx
00000080-000007FF 110yyyxx 10xxxxxx
00000800-0000FFFF 1110yyyy 10yyyyxx 10xxxxxx
00010000-001FFFFF 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
00020000-03FFFFFF 111110aa 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx
04000000-7FFFFFFF 1111110a 10aaaaaa 10zzzzzz 10zzyyyy 10yyyyxx 10xxxxxx

UCS4最多可以表示2^32个字符,但UTF-8最多只能表示2^31个字符,现实当中,Unicode规范根本就还没有规定这么多字符,目前最多也就规定到Plane 16,最多1114111个字符,所以网上关于UTF-8编码,大多截止到4字节:
00000000-0000007F 0xxxxxxx
00000080-000007FF 110yyyxx 10xxxxxx
00000800-0000FFFF 1110yyyy 10yyyyxx 10xxxxxx
00010000-0010FFFF 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx

Unicode规范规定,10FFFE和10FFFF作为内部保留,所以Plane 16支持的最大字符编码是:10FFFD
转换成UTF-8编码是:F48FBFBD

但是,到底这个字符能否正常显示出来,还要看你系统里装的字体到底支持多少字符。

所以,“utf-8里现在已经用到的字符最大编码是多少”这个问题的答案依赖于Unicode规范的版本、具体字体字库的支持。

0x00-0x7F  同ASCII,也不可能作为任何其他多字节UTF-8字符的一部分
0xC0-0xDF  多字节UTF-8字符的开始字节,而且据此可以判断出该UTF-8字符的长度(字节数)
0x80-0xBF  多字节UTF-8字符的跟随字节
0xFE-0xFF  UTF-8未使用

> 字节数 位数 表示
> 1 7 0bbbbbbb
> 2 11 110bbbbb 10bbbbbb
> 3 16 1110bbbb 10bbbbbb 10bbbbbb
> 4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
> 5 26 111110bb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb
> 6 31 1111110b 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb
> 7 36 11111110 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb
> 8 42 11111111 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb 10bbbbbb

typedef uint8 utf8;
//typedef uint16 utf16; // removed typedef to prevent usage, as utf16 is not supported (yet)
typedef uint32 utf32; // return number of code units in a null terminated string
size_type utf_length(const utf8* utf8_str) const
{
size_type cnt = ;
while (*utf8_str++)
cnt++; return cnt;
} // return number of code units in a null terminated string
size_type utf_length(const utf32* utf32_str) const
{
size_type cnt = ;
while (*utf32_str++)
cnt++; return cnt;
}

上面是计算utf8所占的字节数和 utf32 占用的字节数

// return number of utf32 code units required to re-encode given utf8 data as utf32.  len is number of code units in 'buf'.
size_type encoded_size(const utf8* buf, size_type len) const
{
utf8 tcp;
size_type count = ; while (len--)
{
tcp = *buf++;
++count;
size_type size = ; if (tcp < 0x80)
{
}
else if (tcp < 0xE0)
{
size = ;
++buf;
}
else if (tcp < 0xF0)
{
size = ;
buf += ;
}
else
{
size = ;
buf += ;
} if (len >= size)
len -= size;
else
break;
} return count;
}

计算utf8的文字个数

// return the number of utf8 code units required to encode the given utf32 code point
size_type encoded_size(utf32 code_point) const
{
if (code_point < 0x80)
return ;
else if (code_point < 0x0800)
return ;
else if (code_point < 0x10000)
return ;
else
return ;
}

计算ucs4的  转到utf8 的字节个数

size_type encode(const utf8* src, utf32* dest, size_type dest_len, size_type src_len = ) const
{
// count length for null terminated source...
if (src_len == )
{
src_len = utf_length(src);
} size_type destCapacity = dest_len; // while there is data in the source buffer, and space in the dest buffer
for (uint idx = ; ((idx < src_len) && (destCapacity > ));)
{
utf32 cp;
utf8 cu = src[idx++]; if (cu < 0x80)
{
cp = (utf32)(cu);
}
else if (cu < 0xE0)
{
cp = ((cu & 0x1F) << );
cp |= (src[idx++] & 0x3F);
}
else if (cu < 0xF0)
{
cp = ((cu & 0x0F) << );
cp |= ((src[idx++] & 0x3F) << );
cp |= (src[idx++] & 0x3F);
}
else
{
cp = ((cu & 0x07) << );
cp |= ((src[idx++] & 0x3F) << );
cp |= ((src[idx++] & 0x3F) << );
cp |= (src[idx++] & 0x3F);
} *dest++ = cp;
--destCapacity;
} return dest_len - destCapacity;
}

从utf8 转到utf32  即ucs4

size_type encode(const utf32* src, utf8* dest, size_type dest_len, size_type src_len = ) const
{
// count length for null terminated source...
if (src_len == )
{
src_len = utf_length(src);
} size_type destCapacity = dest_len; // while there is data in the source buffer,
for (uint idx = ; idx < src_len; ++idx)
{
utf32 cp = src[idx]; // check there is enough destination buffer to receive this encoded unit (exit loop & return if not)
if (destCapacity < encoded_size(cp))
{
break;
} if (cp < 0x80)
{
*dest++ = (utf8)cp;
--destCapacity;
}
else if (cp < 0x0800)
{
*dest++ = (utf8)((cp >> ) | 0xC0);
*dest++ = (utf8)((cp & 0x3F) | 0x80);
destCapacity -= ;
}
else if (cp < 0x10000)
{
*dest++ = (utf8)((cp >> ) | 0xE0);
*dest++ = (utf8)(((cp >> ) & 0x3F) | 0x80);
*dest++ = (utf8)((cp & 0x3F) | 0x80);
destCapacity -= ;
}
else
{
*dest++ = (utf8)((cp >> ) | 0xF0);
*dest++ = (utf8)(((cp >> ) & 0x3F) | 0x80);
*dest++ = (utf8)(((cp >> ) & 0x3F) | 0x80);
*dest++ = (utf8)((cp & 0x3F) | 0x80);
destCapacity -= ;
} } return dest_len - destCapacity;
}

从utf32 转utf8

utf8 ucs4的更多相关文章

  1. 跨平台字符编码转换GBK、UTF8

    #if (defined _WIN32 || defined _WIN64) # include <windows.h> # include <stdio.h> # inclu ...

  2. std::u32string conversion to/from std::string and std::u16string

    I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have ...

  3. php &#编码/php unicode转码/php &#数字编码

    今天使PHP开发用到了Unicode的编码与解码,将unicode转为中文,再将中文转Unicode这样的操作是非常常见的,所以小编将这两个unicode中文互转函数给作为一个笔记保存起来,非常的简单 ...

  4. 细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4

    1. Unicode与ISO 10646 全世界很多个国家都在为自己的文字编码,并且互不想通,不同的语言字符编码值相同却代表不同的符号(例如:韩文编码EUC-KR中“한국어”的编码值正好是汉字编码GB ...

  5. C++11之后,对源代码增加了UTF8和UCS4的支持(Windows内部使用Unicode,因为nt内核用的是ucs2,那是89年,utf8到了92年才发明出来)

    在C++编程中, 我们常打交道的无非是编辑器和编译器, 对编辑器起来说,我们常遇到就是乱码问题, 比如中文注释显示或是保存不了等, 解决办法就是把你的文件保存成Unicode(UTF8). 对于编译器 ...

  6. C++11与Unicode及使用标准库进行UTF-8、UTF-16、UCS2、UCS4/UTF-32编码转换

    zt https://blog.poxiao.me/p/unicode-character-encoding-conversion-in-cpp11/ Unicode Unicode是计算机领域的一项 ...

  7. [转]utf8编码原理详解

    from : http://blog.csdn.net/baixiaoshi/article/details/40786503 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态 ...

  8. unicode 和 utf8

    关于 unicode utf8 文章来自于 http://blog.csdn.net/tge7618291/article/details/7599902 ascii 主要来表示英文.但是要全世界那么 ...

  9. 趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别(转载)

    从头讲讲编码的故事.那么就让我们找个草堆坐下,先抽口烟,看看夜晚天空上的银河,然后想一想要从哪里开始讲起.嗯,也许这样开始比较好…… 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同 ...

随机推荐

  1. 【TensorFlow】CNN

    tf.nn.conv2d 这个函数的功能是:给定4维的input和filter,计算出一个2维的卷积结果.函数的定义为: def conv2d(input, filter, strides, padd ...

  2. squid搭建http/https代理服务器

    前言:笔者使用的长城宽带,访问国外网站,比如mysql,nginx等站点的速度.......,你懂得,于是想到使用腾讯云主机搭建squid代理服务器,这里搭建的是一般代理服务器,squid代理服务器分 ...

  3. stm32keilIDE遇到的bug

    最进项目中遇到keil中使用sscanf时,采取类正则表达 %*[^/]/%[^@]时不能正确得到的结果,同样的代码在gcc中运行通过.然后又遇到stm32 keil编译器printf带多个参数就卡死 ...

  4. Eigen教程(10)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题.这一篇 ...

  5. python.pandas read and write CSV file

    #read and write csv of pandasimport pandas as pd goog =pd.read_csv(r'C:\python\demo\LiaoXueFeng\data ...

  6. jsp----标签编程(JSTL)

    标签编程简介 JSP的开发是在HTML代码中嵌入了大量的Java代码,但是这样一来会使得JSP页面中充满了Java程序,修改或维护起来非常的不方便, 定义一个简单的标签----空标签 要想实现一个标签 ...

  7. sql逻辑查询 理论知识

    参考并转载http://www.cnblogs.com/bhtfg538/archive/2008/11/25/1341016.html First: (8) SELECT (9) DISTINCT  ...

  8. Android SDK Manager速度慢

    转载自:http://www.cnblogs.com/tc310/archive/2012/12/21/2828450.html Android SDK Manager 无法下载更新,或者更新速度超慢 ...

  9. metrics 开发监控实现jdbc

    Metrics 主要有五大基本组件1:Counter  记录执行次数2:Gauge  获取某个值3:Meter  用来计算事件的速率4:Histogram  可以为数据流提供统计数据. 除了最大值,最 ...

  10. gitlab 地址https://www.gitlab.com.cn/installation/#centos-7

    https://www.gitlab.com.cn/installation/#centos-7 1.安装并配置必要的依赖关系 在CentOS 7(和RedHat / Oracle / Scienti ...