当前位置: 首页 > news >正文

026集——在旧式编码与 Unicode 之间转换(C# 编程指南)——C#学习笔记

 在 C# 中,内存中的所有字符串都是按 Unicode (UTF-16) 编码的。将数据从存储器移动到 string 对象中后,数据将自动转换为 UTF-16。如果数据仅包含从 0 到 127 的 ASCII 值,则此转换无需您执行任何额外的工作。但若源文本包含扩展的 ASCII 字节值(128 到 255),则默认情况下,将根据当前代码页解释扩展字符。若要指定应该根据其他某个代码页解释源文本,请使用 System.Text..::.Encoding 类,如下面的示例所示。

下面的示例演示如何转换按 8 位 ASCII 编码的文本文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
//yngqq@2024年9月3日15:22:45
namespace ConsoleApp1
{class ANSIToUnicode{static void Main(){// Create a file that contains the Greek work ψυχή (psyche) when interpreted by using // code page 737 ((DOS) Greek). You can also create the file by using Character Map // to paste the characters into Microsoft Word and then "Save As" by using the DOS// (Greek) encoding. (Word will actually create a six-byte file by appending "\r\n" at the end.)//桌面自动生成一个txt文件,写入字节。string mypath = @"C:\Users\Administrator\Desktop\greek.txt";System.IO.File.WriteAllBytes(mypath, new byte[] { 0xAF, 0xAC, 0xAE, 0x9E });// Specify the code page to correctly interpret byte values//此外,对于一些常用的字符编码(如UTF-8、Unicode等),C#还提供了更直接的获取方式,如 Encoding.UTF8 或 Encoding.Unicode。然而,对于特定语言或地区的字符编码,如希腊语(代码页737),/则需要使用 GetEncoding 方法指定代码页编号来获取。Encoding encoding = Encoding.GetEncoding(737); //(DOS) Greek code page// Encoding encoding = Encoding.ASCII ;  //也可以用其他编码方式读取                                    string mypath1 = @"C:\Users\Administrator\Desktop\1.txt";//读取字节文件byte[] codePageValues = System.IO.File.ReadAllBytes(mypath);//byte[] codePageValues = System.IO.File.ReadAllBytes(mypath);// Same content is now encoded as UTF-16//用希腊编码737方式读取文本string unicodeValues = encoding.GetString(codePageValues);// Show that the text content is still intact in Unicode string// (Add a reference to System.Windows.Forms.dll)Console.WriteLine(unicodeValues);// Same content "ψυχή" is stored as UTF-8//转换编码后输出到1.txtSystem.IO.File.WriteAllText(mypath1, unicodeValues);// Conversion is complete. Show the bytes to prove the conversion. Console.WriteLine("8-bit encoding byte values:");foreach (byte b in codePageValues)Console.Write("{0:X}-", b);Console.WriteLine("\n");Console.WriteLine("Unicode values:");//需要在桌面建一个2.txt文件string mypath2 = @"C:\Users\Administrator\Desktop\2.txt";string mypath3 = @"C:\Users\Administrator\Desktop\3.txt";string mypath4 = @"C:\Users\Administrator\Desktop\4.txt";//读取桌面的已有文件2.txtstring unicodeString2 = System.IO.File.ReadAllText(mypath2);byte[] code4 = System.IO.File.ReadAllBytes(mypath2);string unicode4 = encoding.GetString(code4);System.IO.File.WriteAllText(mypath4, unicode4);System.Globalization.TextElementEnumerator enumerator =System.Globalization.StringInfo.GetTextElementEnumerator(unicodeString2);//另一种转换方法while (enumerator.MoveNext()){string s = enumerator.GetTextElement();//转换编码方式int i = Char.ConvertToUtf32(s, 0);Console.Write("{0:X}-", i);}Console.WriteLine();System.IO.File.WriteAllText(mypath3, unicodeValues);// Keep the console window open in debug mode.Console.Write("Press any key to exit.");Console.ReadKey();}}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【算法】C++贪心算法解题(单调递增数字、坏了的计算器、合并区间)
  • PostgreSQL 中的 `generate_series` 函数使用
  • MAT:一款针对MSSQL服务器的安全检测与审计工具
  • 【C++】C++智能指针详解
  • VUE3 使用 <transition> 实现组件切换的过渡效果
  • 【日常记录-Linux】WebDriver
  • 如何打造抗冲击的超级电容器?用啥材料好?
  • 大数据技术概述
  • U盘常规数据恢复深度解析:原因、方案与预防策略
  • 文件包含PHP伪协议利用方法
  • c++(list)
  • CSS学习4[重点]
  • 原油市场“闪崩”,国际油价单日下跌超4%!
  • 一. 从Hive开始
  • 坑——fastjson将字符串转到带枚举的java对象
  • [case10]使用RSQL实现端到端的动态查询
  • 【面试系列】之二:关于js原型
  • 2017-09-12 前端日报
  • crontab执行失败的多种原因
  • Github访问慢解决办法
  • in typeof instanceof ===这些运算符有什么作用
  • jquery cookie
  • REST架构的思考
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 从setTimeout-setInterval看JS线程
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 后端_MYSQL
  • 记一次删除Git记录中的大文件的过程
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 利用DataURL技术在网页上显示图片
  • 你真的知道 == 和 equals 的区别吗?
  • 前端
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 设计模式走一遍---观察者模式
  • 收藏好这篇,别再只说“数据劫持”了
  • 思考 CSS 架构
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 湖北分布式智能数据采集方法有哪些?
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • # 计算机视觉入门
  • #我与Java虚拟机的故事#连载03:面试过的百度,滴滴,快手都问了这些问题
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (vue)el-cascader级联选择器按勾选的顺序传值,摆脱层级约束
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (初研) Sentence-embedding fine-tune notebook
  • (分享)自己整理的一些简单awk实用语句
  • (十七)Flink 容错机制
  • (四)js前端开发中设计模式之工厂方法模式
  • (新)网络工程师考点串讲与真题详解
  • (一)80c52学习之旅-起始篇
  • (转)EOS中账户、钱包和密钥的关系
  • (转载)CentOS查看系统信息|CentOS查看命令
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换