C#字节数组与字符串相互转换代码案例

时间:2022-06-10 10:34:07

一、字节数组转换为字符串

using System;

namespace BasicApp
{
class Program
{
public static void Main(string[] args)
{
// 字节数组
byte[] bData = {65,66,67,68};
// 字符串
string str = BytesToString(bData);
// 输出
Console.WriteLine("转换后的字符串为: {0}",str);

string str2 = BytesToString(bData,"GBK");
// 输出
Console.WriteLine("转换后的字符串为: {0}",str2);
Console.ReadKey(true);
}

// 字节数组转换为字符串
public static String BytesToString(byte[] bData)
{
return System.Text.Encoding.GetEncoding(0).GetString(bData);
}

// 字节数组转换为字符串
public static String BytesToString(byte[] bData,string encode)
{
return System.Text.Encoding.GetEncoding(encode).GetString(bData);
}

}
}

字节数组转换为字符串的两种方法:System.Text.Encoding.GetEncoding(0).GetString(bytes)和System.Text.Encoding.GetEncoding("GBK").GetString(bytes)

转载:  http://www.what21.com/sys/view/csharp-net_01_1474182326528.html

二、字节数组转换为ASCII字符串

using System;
using System.Text;

namespace BasicApp
{
class Program
{
public static void Main(string[] args)
{
// 字节数组
byte[] ba = new byte[] { 119,104,97,116,50,49,46,99,111,109 };
// 转换为ASCII的字符
string s = Encoding.ASCII.GetString(ba);
// 打印转换后的内容
Console.WriteLine(s);

Console.ReadKey(true);
}

}
}
C#字节数组转换为ASCII字符的方法:Encoding.ASCII.GetString(bytes);
转载: http://www.what21.com/sys/view/csharp-net_01_1474184697044.html


三、字节数组与Unicode/BigEndianUnicode字符串相互转换

using System;
using System.Text;

namespace BasicApp
{
class Program
{
public static void Main(string[] args)
{
string s = "IT小奋斗";
// 将字符串转换为Unicode数组
byte[] bytes = Encoding.BigEndianUnicode.GetBytes(s);
foreach (byte b in bytes){
Console.Write(b + ",");
}
Console.WriteLine();

// 字节数组
byte[] bytes1 = new byte[] { 73,0,84,0,15,92,75,89,151,101 };
// 转换为 Unicode
string str = Encoding.Unicode.GetString(bytes1);
Console.WriteLine(str);

// 字节数组
byte[] bytes2 = new byte[] { 0,73,0,84,92,15,89,75,101,151 };
// 转换为 BigEndianUnicode
string str2 = Encoding.BigEndianUnicode.GetString(bytes2);
Console.WriteLine(str2);

Console.ReadKey(true);
}

}
}

Unicode编码和BigEndianUnicode编码都使是用2个字节对一个字符进行编码,不同点就是过储存字符的顺序正好相反,即前后颠倒。

转载: http://www.what21.com/sys/view/csharp-net_01_1474185632739.html


四、按照编码(ASCII、UTF7、UTF8、Unicode、BigEndianUnicode和UTF32)将字符串转换为字节数组

using System;
using System.IO;
using System.Text;

namespace BasicApp
{
class Program
{
public static void Main(string[] args)
{
string file = "c://what21.txt";
using (StreamWriter output = new StreamWriter(file))
{
// 写出文件的字符串
string srcString = "IT小奋斗";

// 安装不同的编码转换为数组
byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
byte[] utf7String = Encoding.UTF7.GetBytes(srcString);
byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
byte[] unicodeString = Encoding.Unicode.GetBytes(srcString);
byte[] beUnicodeString = Encoding.BigEndianUnicode.GetBytes(srcString);
byte[] utf32String = Encoding.UTF32.GetBytes(srcString);

// 格式化输出编码数组
output.WriteLine("ASCII Bytes: {0}", BitConverter.ToString(asciiString));
output.WriteLine("UTF-7 Bytes: {0}", BitConverter.ToString(utf7String));
output.WriteLine("UTF-8 Bytes: {0}", BitConverter.ToString(utf8String));
output.WriteLine("Unicode Bytes: {0}", BitConverter.ToString(unicodeString));
output.WriteLine("BigEndianUnicode Bytes: {0}", BitConverter.ToString(beUnicodeString));
output.WriteLine("UTF32 Bytes: {0}", BitConverter.ToString(utf32String));

// Console输出
Console.WriteLine(BitConverter.ToString(utf8String));
Console.WriteLine(BitConverter.ToString(utf7String));
Console.WriteLine(BitConverter.ToString(asciiString));
Console.WriteLine(BitConverter.ToString(unicodeString));
Console.WriteLine(BitConverter.ToString(beUnicodeString));
Console.WriteLine(BitConverter.ToString(utf32String));
}
Console.ReadKey(true);
}

}
}

将一个字符串,按照编码ASCII、UTF7、UTF8、Unicode、BigEndianUnicode和UTF32分别转换成编码后的数组,经过BitConverter类格式化后,保存到文件并Console打印。

转载: http://www.what21.com/sys/view/csharp-net_01_1474186676521.html