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 ...
随机推荐
- Qt Linguist介绍
简介 Qt提供了一款优秀的支持Qt C++和Qt Quick应用程序的翻译工具.发布者.翻译者和开发者可以使用这款工具来完成他们的任务. 发布者:承担了全面发布应用程序的责任.通常,他们协调开发者和翻 ...
- 51nod1364 最大字典序排列
不断的在cur的后面找最大的符合条件的数扔到cur的前面. 用线段树维护操作就可以了. #include<cstdio> #include<cstring> #include& ...
- UVa 10791 Minimum Sum LCM【唯一分解定理】
题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...
- linux下编译软件通用方法(memcached为例)
1)到软件的官网或其他网站下载软件的源码包 2)解压源码包,并切换到源码目录中 3)使用./configure --help查询配置帮助,里面可能会有安装指南(Installation directo ...
- DOM对象常用对象的方法和属性
HTML文档中的常用节点类型: 接口 nodeType 备注 Element 1 元素节点 Text 3 文本节点 Document 9 Document Comment 8 注释文本 Docum ...
- 关于ios越狱开发的那些事
也许吧,每每接触某些新东西的时候,都有点犯晕吧,这不是应该要的. 第一次接触ios越狱开发,也是这样吧.这篇主要是从无到有的说一下ios越狱的开发,网上很多的教程大部门都比较旧了吧,放在新设备上总是出 ...
- python练习程序(批量重命名)
# -*- coding: cp936 -*- import sys,os,string d=0; path="F://test" srcfile=os.listdir(path) ...
- 【Sass初级】开始使用Sass和Compass
转自:http://www.w3cplus.com/preprocessor/beginner/getting-started-with-sass-and-compass.html 如果你的朋友.同事 ...
- phonegap 使用极光推送实现消息推送
最近一直在研究各种推送,ios的由于是apns,比较容易实现,但是andriod的就比较麻烦.后来看了很多解决方案,gcm明显是不行的,其他的方案更是一头雾水,而且需要做第二次开发,太麻烦,后来就选择 ...
- Jquery Mobile设计Android通讯录第二章
本文是jQuery Mobile设计Android通讯录系统教程的第二篇,在上一篇教程中(http://publish.itpub.net/a2011/0517/1191/000001191561.s ...