C#字节数组转换成字符串
C#字节数组转换成字符串
如果还想从 System.String 类中找到方法进行字符串和字节数组之间的转换,恐怕你会失望了。为了进行这样的转换,我们不得不借助另一个类:System.Text.Encoding。该类提供了 bye[] GetBytes(string) 方法将字符串转换成字节数组,还提供了 string GetString(byte[]) 方法将C#字节数组转换成字符串。
System.Text.Encoding 类似乎没有可用的构造函数,但我们可以找到几个默认的 Encoding,即 Encoding.Default(获取系统的当前 ANSI 代码页的编码)、Encoding.ASCII(获取 7 位 ASCII 字符集的编码)、Encoding.Unicode(获取采用 Little-Endian 字节顺序的 Unicode 格式的编码)、Encoding.UTF7(获取 UTF-7 格式的编码)、Encoding.UTF8(获取 UTF-8 格式的编码) 等。这里主要说说 Encoding.Default 和 Encoding.Unicode 用于转换的区别。
在字符串转换到字节数组的过程中,Encoding.Default 会将每个单字节字符,如半角英文,而把每个双字节字符,如汉字。而 Encoding.Unicode 则会将它们都转换成两个字节。我们可以通过下列简单的了解一下转换的方法,以及使用 Encoding.Default 和 Encodeing.Unicode 的区别:
- private void TestStringBytes() {
- string s = "C#语言";
- byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
- byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s);
- string t1 = "", t2 = "";
- foreach (byte b in b1) {
- t1 += b.ToString("") + " ";
- }
- foreach (byte b in b2) {
- t2 += b.ToString("") + " ";
- }
- this.textBox1.Text = "";
- this.textBox1.AppendText("b1.Length = " + b1.Length + "\n");
- this.textBox1.AppendText(t1 + "\n");
- this.textBox1.AppendText("b2.Length = " + b2.Length + "\n");
- this.textBox1.AppendText(t2 + "\n");
- }
运行结果如下,不说详述,相信大家已经明白了。
- b1.Length = 6
- 67 35 211 239 209 212
- b2.Length = 8
- 67 0 35 0 237 139 0 138
将C#字节数组转换成字符串,使用 Encoding 类的 string GetString(byte[]) 或 string GetString(byte[], int, int) 方法,具体使用何种 Encoding 还是由编码决定。在 TestStringBytes() 函数中添加如下语句作为实例:
- byte[] bs = {97, 98, 99, 100, 101, 102};
- string ss = System.Text.Encoding.ASCII.GetString(bs);
- this.textBox1.AppendText("The string is: " + ss + "\n");
运行结果为:The string is: abcdef
C#字节数组转换成字符串的更多相关文章
- 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?
100怎么变成100.00?
- 怎样把php数组转换成字符串,php implode()
实例代码 一维数组转换成字符串代码! <?php $arr1=array("shu","zhu","1"); $c=implode(& ...
- 在Ajax中将数组转换成字符串(0517-am)
一.如何在Ajax中将数组转换成字符串 1. 主页面; <head> <meta http-equiv="Content-Type" content=" ...
- Java中如何将字符串数组转换成字符串
如果将“字符串数组”转换成“字符串”,只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb ...
- js冒泡法和数组转换成字符串示例代码
将数组转换成字符串的方法有很多,讲解下js冒泡法的使用.js代码: //js冒泡法与数据转换为字符串的例子 //整理:www.jbxue.com window.onload = function(){ ...
- 【JS】jQuery中将数组转换成字符串join()和push()使用
1.push()将元素依次添加至数组:2.join()将数组转换成字符串,里面可以带参数分隔符,默认[,] <script type = text/javascript> $(docume ...
- js 中 json.stringfy()将对象、数组转换成字符串
json.stringfy()将对象.数组转换成字符串 var student = new Object(); student.name = "Lanny"; student.ag ...
- JS - 字符串转换成数组,数组转换成字符串
1.字符串转换成数组: var arr = "1, 2, 3, 4, 5, 6"; arr.split(","); // ["1",&quo ...
- js冒泡法和数组转换成字符串
js代码: window.onload = function(){ var mian = document.getElementById( "mian" ); var mian1 ...
随机推荐
- Windows Server 2008 R2 主域控制器委派DNS到子域控控制器
一.实验网络拓扑图: 二.实现目的: 子域控制器的域用户能查询到主域控制器的DNS服务器的A记录,主域控制器的域用户也能查询到子域控制器的DNS服务器的A记录. 标注:此章节不讲解域控和DNS服务器的 ...
- PAT复杂度_最大子列和问题、最大子列和变种
01-复杂度1. 最大子列和问题 给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j ...
- Leetcode: Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- ferret32位安装
首先在网上找到解决方案: 1.添加对32位的支持 dpkg --add-architecture i386 2.更新 apt-get clean && apt-get update & ...
- Pointcut is malformed: Pointcut is not well-formed: expecting 'identifier' at character position 0 ^
错误提示: 解决方法:指定execution 在执行目标方法之前指定execution 例如: import org.aspectj.lang.annotation.Aspect; import or ...
- sdk命令
SDK命令 常用sdk命令 开启adb服务:adb start -server 关闭adb服务:adb kill -server 查看模拟器/真机:adb devices 安装/卸载/运行程序: 安装 ...
- js事件监听/鼠标滚轮/行为/冒泡/键盘的兼容性写法
addEvent:function(el,type,fn,capture) { if (window.addEventListener) { if (type === "mousewheel ...
- rc.local文件
rc.local用于自启动一些服务. 查看有哪些自启动服务: cat /etc/rc.local
- 前端 时间个性化 插件 jquery.timeago.js
关键词 : 时间格式化 刚刚 N分钟前 N小时前 N天前 N月前 N年前 MM-dd hh:mm 或者 yyyy-MM-dd 前端: <span class="time" ...
- mysql 卸载 linux
root@localhost ~]# rpm -qa | grep -i mysqlMySQL-client-5.5.52-1.linux2.6.x86_64MySQL-server-5.5.52-1 ...