如何在Byte[]和String之间进行转换
源自C#与.NET程序员面试宝典。
如何在Byte[]和String之间进行转换?
比特(b):比特只有0 1,1代表有脉冲,0代表无脉冲。它是计算机物理内存保存的最基本单元。
字节(B):8个比特,0—255的整数表示
编码:字符必须编码后才能被计算机处理。早期计算机使用7为AscII编码,为了处理汉字设计了中文简体GB2312和big5
字符串与字节数组之间的转换,事实上是现实世界的信息和数字世界信息之间的转换,势必涉及到某种编码方式,不同的编码方式将导致不同的转换结果。C#中常使用System.Text.Encoding来管理常用的编码。下面直接上代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ByteToString
{
class Program
{
static void Main(string[] args)
{
string str = "鞠哥真帅!"; //使用UTF编码。。。
Byte[] utf8 = StrToByte(str, Encoding.UTF8);
//估计C#当时设计时没有中文简体,这是后来中国搞的?
Byte[] gb2312 = StrToByte(str,Encoding.GetEncoding("GB2312")); Console.WriteLine("这是UTF8(鞠哥真帅),长度是:{0}",utf8.Length);
foreach (var item in utf8)
{
Console.Write(item);
} Console.WriteLine("\n\n这是gb2312(鞠哥真帅),长度是:{0}",gb2312.Length);
foreach (var item in gb2312)
{
Console.Write(item);
} //用utf8编码的字节数组转换为str
string utf8Str = ByteToStr(utf8,Encoding.UTF8);
string gb2312Str = ByteToStr(gb2312,Encoding.GetEncoding("GB2312")); Console.WriteLine("\n\nutf8: {0}",utf8Str);
Console.WriteLine("gb2312: {0}",gb2312Str);
Console.ReadKey(); } //C#通常使用System.Text.Encoding编码 //字符串转数组
static Byte[] StrToByte(string str, Encoding encoding)
{
return encoding.GetBytes(str);
} //数组转换字符串
static String ByteToStr(Byte[] bt,Encoding encoding)
{
return encoding.GetString(bt);
} }
}
如何在Byte[]和String之间进行转换的更多相关文章
- golang []byte和string的高性能转换
golang []byte和string的高性能转换 在fasthttp的最佳实践中有这么一句话: Avoid conversion between []byte and string, since ...
- Python3中内置类型bytes和str用法及byte和string之间各种编码转换,python--列表,元组,字符串互相转换
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 python--列表,元组,字符串互相转换 列表,元组和字符串python中有三个内建函数:,他们之间的互相转换 ...
- C#中char[]与string之间的转换;byte[]与string之间的转化
目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...
- C#中char[]与string之间的转换
string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string st ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- Byte[]和BASE64之间的转换
一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...
- c# String ,String[] 和 List<String>之间的转换
C#对字符串进行处理时,经常需要进行String,String[]和List<String>之间的转换 本文分析一下它们的差异和转换 一. 1. String > String[] ...
- 基本数据类型、包装类、String之间的转换
package 包装类; /** *8种基本数据类型对应一个类,此类即为包装类 * 基本数据类型.包装类.String之间的转换 * 1.基本数据类型转成包装类(装箱): * ->通过构造器 : ...
- java字符数组char[]和字符串String之间的转换
java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...
随机推荐
- Android引导界面
一.所需素材 很有必要整理一下,里面附带友盟的社会化分享组件,我就不去掉了. 二.代码 import com.umeng.update.UmengUpdateAgent; import a ...
- HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题
先设置4个: [csharp] webrequest.ServicePoint.Expect100Continue = false; //是否使用 Nagle 不使用 提高效率 webrequest. ...
- 李洪强漫谈iOS开发[C语言-030]-逻辑运算符
- [转贴]gsoap使用心得!
最近换了个工作环境,现在在大望路这边上班,呵,刚上班接到的任务就是熟悉gsoap!废话少说,现在开始gSoap学习! gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在W ...
- ANDROID_MARS学习笔记_S05_005_方向传感器
import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import ...
- 【转】SourceTree的简单使用
原文网址:http://blog.csdn.net/u011439289/article/details/42126507 今天开始参与公司项目的代码编写,公司内部采用的是gitlib,所以用到了So ...
- 【转】c++重载、覆盖、隐藏——理不清的区别
原文网址:http://blog.sina.com.cn/s/blog_492d601f0100jqqm.html 再次把林锐博士的<高质量c++编程指南>翻出来看的时候,再一次的觉得这是 ...
- 【转】Android4.4(MT8685)源码蓝牙解析--BLE搜索
原文网址:http://blog.csdn.net/u013467735/article/details/41962075 BLE:全称为Bluetooth Low Energy.蓝牙规范4.0最重要 ...
- HDOJ -- 1015
1.DFS #include<cmath> #include<cstdio> #include<cstdlib> #include<string> #i ...
- zoj 3805 Machine
Machine Time Limit: 2 Seconds Memory Limit: 65536 KB In a typical assembly line, machines are c ...