如何在 .net 中生成 UTF-8 字符集中的所有字符
- 作者: 速览天下
- 来源: 51数据库
- 2023-02-13
问题描述
我的任务是生成 UTF-8 字符集中的所有字符,以测试系统如何处理每个字符.我对字符编码没有太多经验.我要尝试的方法是增加一个计数器,然后尝试将那个以十为基数的数字转换为等效的 UTF-8 字符,但到目前为止,我还没有在 C# 3.5 中找到一种有效的方法
I have been given the task of generating all the characters in the UTF-8 character set to test how a system handles each of them. I do not have much experience with character encoding. The approaching I was going to try was to increment a counter, and then try to translate that base ten number into it's equivalent UTF-8 character, but so far I have no been able to find an effective way to to this in C# 3.5
任何建议将不胜感激.
推荐答案
System.Net.WebClient client = new System.Net.WebClient(); string definedCodePoints = client.DownloadString( "http://www.51sjk.com/Upload/Articles/1/0/343/343162_20230213091656105.txt"); System.IO.StringReader reader = new System.IO.StringReader(definedCodePoints); System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); while(true) { string line = reader.ReadLine(); if(line == null) break; int codePoint = Convert.ToInt32(line.Substring(0, line.IndexOf(";")), 16); if(codePoint >= 0xD800 && codePoint <= 0xDFFF) { //surrogate boundary; not valid codePoint, but listed in the document } else { string utf16 = char.ConvertFromUtf32(codePoint); byte[] utf8 = encoder.GetBytes(utf16); //TODO: something with the UTF-8-encoded character } }
上面的代码应该遍历当前分配的 Unicode 字符.您可能想要在本地解析 UnicodeData 文件并修复我遇到的任何 C# 错误制作.
The above code should iterate over the currently assigned Unicode characters. You'll probably want to parse the UnicodeData file locally and fix any C# blunders I've made.
当前分配的 Unicode 字符集小于可以定义的集.当然,当您打印出其中一个字符时,您是否看到一个字符取决于许多其他因素,例如字体和它在发送到您的眼球之前会通过的其他应用程序.
The set of currently assigned Unicode characters is less than the set that could be defined. Of course, whether you see a character when you print one of them out depends on a great many other factors, like fonts and the other applications it'll pass through before it is emitted to your eyeball.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源