C#中字符串与byte[]相互转换
字符串转换为byte[]
给定一个string,转换为byte[],有以下几种方法。
方法1:
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
方法2:
var array = Encoding.Default.GetBytes(input);
//这里面的编码集可以是:Default、ASCII、Unicode、UTF8等。
为了查看以上两种方法的区别,我写了下面一段测试代码。
using System;
using System.Text;
namespace BytesTest
{
class Program
{
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static void Convert()
{
string input = "赵";
Console.WriteLine("default");
var array = Encoding.Default.GetBytes(input);
foreach (var inst in array)
{
Console.Write(inst);
Console.Write("\t");
}
Console.WriteLine();
Console.WriteLine("ASCII");
array = Encoding.ASCII.GetBytes(input);
foreach (var inst in array)
{
Console.Write(inst);
Console.Write("\t");
}
Console.WriteLine();
Console.WriteLine("Unicode");
array = Encoding.Unicode.GetBytes(input);
foreach (var inst in array)
{
Console.Write(inst);
Console.Write("\t");
}
Console.WriteLine();
Console.WriteLine("UTF8");
array = Encoding.UTF8.GetBytes(input);
foreach (var inst in array)
{
Console.Write(inst);
Console.Write("\t");
}
Console.WriteLine();
Console.WriteLine("method");
array = GetBytes(input);
foreach (var inst in array)
{
Console.Write(inst);
Console.Write("\t");
}
Console.WriteLine();
}
static void Main(string[] args)
{
Convert();
Console.Read();
}
}
}
输出
default
213 212
ASCII
63
Unicode
117 141
UTF8
232 181 181
method
117 141
不同编码集输出不同不解释了。
可以看到,方法1用的是unicode的方式。
byte[]转换为字符串
方法1
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
方法2
var str1 = Encoding.Unicode.GetString(arr);
C#中字符串与byte[]相互转换的更多相关文章
- java 中 image 和 byte[] 相互转换
java 中 image 和 byte[] 相互转换可恶的…………其实也挺好的 只是把好不容易写出来的东西记下来,怕忘了…… 下面,我来介绍一个简单的 byte[] to image, 我们只需要 ...
- C#中string和byte[]相互转换问题解决
本来想讲string转换为byte数组,通过在VS上打 ‘str. “来找,结果半天没发现跳出来的函数中有想要的,哭瞎 /(ㄒoㄒ)/~~ 这回将两种情况都记下来了.... string ---> ...
- Java中字符串和byte数组之间的相互转换
1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...
- C/C++中字符串与数字相互转换
数字转字符串: 用C++的streanstream: #include <sstream> #Include <string> string num2str(double i) ...
- C#中字节数组(byte[])和字符串相互转换
转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...
- 16进制字符串和byte数组进行相互转换\将10进制转换为任意进制
16进制字符串和byte数组进行相互转换 简介 1个byte对应8个bit,16进制使用4个bit,所以一个byte转成16进制,占用两位. JAVA代码 private static final c ...
- JAVA中文件与Byte数组相互转换的方法
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...
- Python中字符串与字节之间相互转换
Python中字符串与字节之间相互转换 a = b"Hello, world!" # bytes object b = "Hello, world!" # ...
- C#中字节数组byte[]和字符串string类型的相互转换
C#中字节数组byte[]和字符串string类型的相互转换: string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBy ...
随机推荐
- java MVC设计模式
MVC(Model View Control)模型-视图-控制器 一.MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是 ...
- Object-C 内存管理及对象
关于OC 的内存管理是使用 引用计数的方式 进行管理的引用计数可以使用 办公室的开关灯 来说明 如下图与 OC对象 对应如下
- 转载:NPOI导出到Excel表格
NPOI开发包下载:http://pan.baidu.com/share/link?shareid=4025220936&uk=2837994235 需要命名空间 using NPOI.HSS ...
- 免费Gif图片录制工具
/************************************************************************* * 免费Gif图片录制工具 * 说明: * 最近在 ...
- acdream 1686 梦醒(时钟重合)
Problem Description 娜娜离开了这个王国,走向远方,在旷野上,娜娜看到了一个大时钟,上面的时针分针秒针都在缓缓转动,那只挥着翅膀的天使又出现了,天使说:“外面天已经亮了,娜娜你别睡过 ...
- 【自动化测试】Selenium 2.0 学习笔记
定位下拉框元素,要记得先把鼠标挪过去再进行定位. 定位元素用的是find_element_by.... python的模块化还要去思考下 unittest框架使用的时候,py文件命名不要用unitte ...
- RequireJS进阶(一) 转
为了应对日益复杂,大规模的JavaScript开发.我们化整为零,化繁为简.将复杂的逻辑划分一个个小单元,各个击破.这时一个项目可能会有几十个甚至上百个JS文件,每个文件为一个模块单元.如果上线时都是 ...
- 部署K2 Blackpearl流程时出错(与基础事务管理器的通信失败或Communication with the underlying transaction manager has failed.
转:http://www.cnblogs.com/dannyli/archive/2011/12/01/2270222.html 亲,在部署K2流程是,是否遇到这个错误(以下是中.英文错误信息) 中文 ...
- rtree
https://zh.wikipedia.org/wiki/R%E6%A0%91 http://blog.csdn.net/jiqiren007/article/details/5377750 htt ...
- 三种map的循环
for(Map.Entry<String, List> entry : map.entrySet()) { System.out.println(entry.getKey()); List ...