数组Byte [] 和 string 相互转换
using System;
using System.Collections.Generic;
using System.Text; namespace NET.MST.Fourth.StringByte
{
class StringByte
{
static void Main(string[] args)
{
String s = "我是字符串,I am string"; //字节数组转换到字符串
Byte[] utf8 = StringToByte(s,
Encoding.UTF8);
Byte[] gb2312 = StringToByte(s,
Encoding.GetEncoding("GB2312"));
Byte[] unicode = StringToByte(s,
Encoding.Unicode);
Console.WriteLine(utf8.Length);
Console.WriteLine(gb2312.Length);
Console.WriteLine(unicode.Length); //转换回字符串
Console.WriteLine(ByteToString(utf8,
Encoding.UTF8));
Console.WriteLine(ByteToString(gb2312,
Encoding.GetEncoding("GB2312")));
Console.WriteLine(ByteToString(unicode,
Encoding.Unicode));
Console.Read();
}
static Byte[] StringToByte(String s, Encoding encoding)
{
return encoding.GetBytes(s);
}
static String ByteToString(Byte[] b, Encoding encoding)
{
return encoding.GetString(b);
}
}
}
数组Byte [] 和 string 相互转换的更多相关文章
- C#中字节数组(byte[])和字符串相互转换
转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...
- golang []byte 和 string相互转换
原文链接:golang []byte和string相互转换 测试例子 package main import ( "fmt" ) func main() { str2 := &qu ...
- C#-----字节数组(byte[])和字符串相互转换
Encoding类 表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...
- java的byte[]与String相互转换
String转byte[] byte[] sInput = new byte[0]; try { // 可以指定编码,默认也只UTF-8 sInput = "这是内容".getBy ...
- golang []byte和string相互转换
测试例子 package main import ( "fmt" ) func main() { str2 := "hello" ...
- C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换
定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 复制代码 代码如下: (1)byte[] bt=System.Text.Encoding.Default.GetB ...
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- C# 跨线程调用form控件技巧及byte[]与string型相互转换
跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginI ...
随机推荐
- What’s that ALUA exactly?
What’s that ALUA exactly? 29 September, 20098 Comments Of course by now we have all read the excelle ...
- Intent使用方法
显示Intent 通过构造函数的重载,创建Intent对象,并用startActivity()启动目标活动. 目标活动需要在AndroidManifest.xml中注册 ...... Intent i ...
- mysql安装与基本管理
一.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...
- 第五章 bean的加载(待续)
·············
- 「小程序JAVA实战」 小程序wxss样式文件的使用(七)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-07/ 细说下微信小程序的wxss样式文件.源码:https://github.com/liming ...
- Java微信公众平台开发(十六)--微信网页授权(OAuth2.0授权)获取用户基本信息
转自:http://www.cuiyongzhi.com/post/78.html 好长时间没有写文章了,主要是最近的工作和生活上的事情比较多而且繁琐,其实到现在我依然还是感觉有些迷茫,最后还是决定静 ...
- 动态规划——最长不下降子序列(LIS)
最长不降子序列是这样一个问题: 下面介绍动态规划的做法. 令 dp[i] 表示以 A[i] 结尾的最长不下降序列长度.这样对 A[i] 来说就会有两种可能: 如果存在 A[i] 之前的元素 A[j] ...
- HtmlHelper扩展
扩展 HtmlHelper类 public static class MyHtmlHelper { //扩展方法 //静态类,静态方法,this关键字 //调用方法<%=Html.MyLabel ...
- 张超超OC基础回顾04_实例变量修饰(@public),点语法,self关键字,多态,继承
零.实例变量修饰符 /* @public 就是实例变量修饰符 @public >可以在其它类中访问被public修饰的成员变量 >也可以在本类中访问被public修饰的成员变量 >可 ...
- 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数
[抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...