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 相互转换的更多相关文章

  1. C#中字节数组(byte[])和字符串相互转换

    转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...

  2. golang []byte 和 string相互转换

    原文链接:golang []byte和string相互转换 测试例子 package main import ( "fmt" ) func main() { str2 := &qu ...

  3. C#-----字节数组(byte[])和字符串相互转换

       Encoding类  表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...

  4. java的byte[]与String相互转换

    String转byte[] byte[] sInput = new byte[0]; try { // 可以指定编码,默认也只UTF-8 sInput = "这是内容".getBy ...

  5. golang []byte和string相互转换

    测试例子 package main   import (     "fmt" )   func main() {     str2 := "hello"     ...

  6. C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换

    定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 复制代码 代码如下: (1)byte[] bt=System.Text.Encoding.Default.GetB ...

  7. 字符串string和内存流MemoryStream及比特数组byte[]互转

    原文:字符串string和内存流MemoryStream及比特数组byte[]互转   字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...

  8. c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

    字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...

  9. C# 跨线程调用form控件技巧及byte[]与string型相互转换

    跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginI ...

随机推荐

  1. FPGA市场潜力有几多?

    FPGA市场未来成长潜力 本文来源:DIGITIMES 2014年FPGA市场规模为52.7亿美元,据Green Mountain Outlook报导,研调机构Global Market Insigh ...

  2. Dynamics CRM 2011 WebResources

    Type Limitation Capabilities Usage Images JPG,   PNG, GIF, ICO Custom entity icons Icons for custom ...

  3. call()和apply()的认知

    apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性.  Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 arg ...

  4. ubuntu安装nginx踩坑

    ubuntu安装nginx 安装nginx tar -zxvf nginx-1.15.5.tar.gz -C /usr/local/src 解压 cd /usr/local/src/nginx-1.1 ...

  5. python web指纹获取加目录扫描加端口扫描加判断robots.txt

    前言: 总结上几次的信息收集构造出来的. 0x01: 首先今行web指纹识别,然后在进行robots是否存在.后面是目录扫描 然后到使用nmap命令扫描端口.(nmap模块在windows下使用会报停 ...

  6. Django Rest Framework 4

    目录 一.分页 二.视图 三.路由 四.渲染器 一.分页 试问如果当数据量特别大的时候,你是怎么解决分页的? 方式a.记录当前访问页数的数据id 方式b.最多显示120页等 方式c.只显示上一页,下一 ...

  7. javascript中的垃圾回收

    1引用计数垃圾回收 核心:跟踪记录对象被引用的次数.思路是如果一个对象A被赋值给了一个变量v,则该对象A的引用计数值加1,如果变量v又被赋予其他值了,比如a="str",则该对象A ...

  8. python 模块 optparse

    optparse,是一个能够让程式设计人员轻松设计出简单明了.易于使用.符合标准的Unix命令列程式的Python模块.生成使用和帮助信息. 下面是一个简单的示例: import optparse p ...

  9. Spring Cloud Eureka 5 (服务发现与消费-简单的robbin使用)

    通过上述介绍,我们已经有了服务注册中心和服务提供者 下面我们来尝试构建一个服务的消费者 它要完成两个功能,发现服务和消费服务,其中发现服务由eureka客户端完成,消费服务由ribbon完成. rib ...

  10. Linux开机启动详解

    Linux开机启动程序详解 我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤 ...