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 的区别:

  1. private void TestStringBytes() {
  2. string s = "C#语言";
  3. byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
  4. byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s);
  5. string t1 = "", t2 = "";
  6. foreach (byte b in b1) {
  7. t1 += b.ToString("") + " ";
  8. }
  9. foreach (byte b in b2) {
  10. t2 += b.ToString("") + " ";
  11. }
  12. this.textBox1.Text = "";
  13. this.textBox1.AppendText("b1.Length = " + b1.Length + "\n");
  14. this.textBox1.AppendText(t1 + "\n");
  15. this.textBox1.AppendText("b2.Length = " + b2.Length + "\n");
  16. this.textBox1.AppendText(t2 + "\n");
  17. }

运行结果如下,不说详述,相信大家已经明白了。

  1. b1.Length = 6
  2. 67 35 211 239 209 212
  3. b2.Length = 8
  4. 67 0 35 0 237 139 0 138

将C#字节数组转换成字符串,使用 Encoding 类的 string GetString(byte[]) 或 string GetString(byte[], int, int) 方法,具体使用何种 Encoding 还是由编码决定。在 TestStringBytes() 函数中添加如下语句作为实例:

  1. byte[] bs = {97, 98, 99, 100, 101, 102};
  2. string ss = System.Text.Encoding.ASCII.GetString(bs);
  3. this.textBox1.AppendText("The string is: " + ss + "\n");

运行结果为:The string is: abcdef

时间: 2024-08-03 18:36:18

C#字节数组转换成字符串的相关文章

Java中如何将字符串数组转换成字符串

如果将"字符串数组"转换成"字符串",只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb = new StringBuffer(); String prefixStr = str.substring(0,str.indexOf("(")); String subStr = str.substring(str.indesOf("

在Ajax中将数组转换成字符串

主页面; <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ajax查表格</title> <script src="../jquery-1.11.2.min.js"> </script> </head> <body> <div&

怎样把php数组转换成字符串,php implode()

实例代码 一维数组转换成字符串代码! <?php $arr1=array("shu","zhu","1"); $c=implode("##",$arr1); echo $c;  //shu##zhu##1 ?> 二维数组转换成字符串代码 <?php $arr=array(array("hu","peng","123456")); $b=implode

android byte字节数组转换十六进制字符串(物联网开发总结)

想起前段时间的物联网的外包开发,经常遇到通过wifi接受的数据,要通过转换成十六进制字符串,或者最后又是十进制数据.都是根据双方的协议来开发的.那么我发送过去的数据也需要,经过特殊转换成byte字节发过去,硬件那边收到不至于乱码的数据. 1.硬件调试发给android这边是十六进制数据 原始数据:68 38 38 68 A 72 78 55 34 12 43 23 01 07 Y 00 00 00 0C 13 78 56 34 12 0C 3B 78 34 12 0C 26 78 56 34 1

js中字符串转换成数组,数组转换成字符串的实现

数组转字符串(将数组元素用某个字符连接成字符串) var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 字符串转数组(将字符串按某个字符切割成若干个字符串,并以数组形式返回) var s = "abc,abcd,aaa"; ss = s.split(",");// 在每个逗号(,)处进行分解.

java中如何将字符串数组转换成字符串(转)

如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"}; StringBuffer sb = new StringBuffer(); for(int i = 0; i < str.length; i++){ sb. append(str[i]); } String s = sb.toString(); 如果是 “字符数组” 转 “字符串” 可以通过下边的方

js join()函数将数组转换成字符串

join() 方法用于把数组中的所有元素放入一个字符串.作用是将数组转换为字符串,其作用和toString()相同. 元素是通过指定的分隔符进行分隔的. 例如: var asp=['H','ell','o']; a=asp.join('#'); #:表示以什么符号链接 a=asp.join('');  输出:Hello console.log(a);  输出:H#ell#o 用途:html代码字符串的组合 var topHead =[ '<span class="user">

以下代码将字节数转换成字符串形式的尺寸描述

#include <stdio.h> #include <stdlib.h> #include <string.h> #define SECTOR_SIZE_DEFAULT 512LL #define KILOBYTE_SIZE 1000LL #define MEGABYTE_SIZE 1000000LL #define GIGABYTE_SIZE 1000000000LL #define TERABYTE_SIZE 1000000000000LL #define KI

C#中字节数组(byte[])和字符串相互转换

转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string"; byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); 2.字节数组换成字符串: byte[] byteArray = 通过某种方式获取到的字节数组 string str = System.Text.Encoding.Default