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 ...
随机推荐
- tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案
错误代码如下: 严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException ...
- jint
nuget地址 https://www.nuget.org/packages/Jint/ github上源代码 https://github.com/sebastienros/jint
- Asp.Net判断字符是否为汉字的方法大全
判断一个字符是不是汉字通常有三种方法: 第一种用 ASCII 码判断,缺点:把全角逗号“,”当汉字处理 第二种用汉字的 UNICODE 编码范围判 断, 第三种用正则表达式判断 1.用ASCII码判断 ...
- 作业调度框架 Quartz.NET 2.0 StepByStep
注:目前网上诸多介绍Quartz.net的文章,甚至Quartz.net官网上的Tutorial都是1.0版本的,而这个项目在2.0版本对项目进行了比较大规模的修改,使得原有的很多例子都不能运行,故写 ...
- 广义线性模型 GLM
Logistic Regression 同 Liner Regression 均属于广义线性模型,Liner Regression 假设 $y|x ; \theta$ 服从 Gaussian 分布,而 ...
- 标准IO库函数复习
打开文件,打开文件一定要成对写,养成好习惯很重要.比如 fopen()fclose<ol> <li>fopen()</li> <pre lang=" ...
- Java中Volatile关键字详解
一.基本概念 先补充一下概念:Java并发中的可见性与原子性 可见性: 可见性是一种复杂的属性,因为可见性中的错误总是会违背我们的直觉.通常,我们无法确保执行读操作的线程能适时地看到其他线程写入的值, ...
- ssh 或者 scp 无需输入密码的解决办法
这里假设主机A(192.168.100.3)用来获到主机B(192.168.100.4)的文件. 在主机A上执行如下命令来生成配对密钥: ssh-keygen -t rsa 遇到提示回车默认即 ...
- Datawindow.net+access数据窗口制作方法
1) 数据字典:采用SQLServer桌面程序来创建数据字典.配置正确的pbl文件生成输入列表. 在SQLServers查询器中执行select * from pbcatedt where pbe_n ...
- Linux下信号的简单使用
1,1个main, 包含2个while, 不要被两个while中的sleep所迷惑,这里只有main()这一个主线程(进程)在运行,程序会按照自上而下顺序执行. 遇到第1个while循环中的sleep ...