参考:

http://www.cnblogs.com/zxx193/p/3605238.html?utm_source=tuicool

http://www.cnblogs.com/freeliver54/p/3430956.html

http://www.cnblogs.com/simhare/archive/2007/07/18/821938.html

定义string变量为str,内存流变量为ms,比特数组为bt

1.字符串=>比特数组

(1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串");

(2)byte[] bt=Convert.FromBase64String("字符串");

补充:

System.Text.Encoding.Unicode.GetBytes(str);
System.Text.Encoding.UTF8.GetBytes(str);
System.Text.Encoding.GetEncoding("gb2312").GetBytes(str); //指定编码方式
string str = "中国?ss123?";
byte[] bytes = System.Text.Encoding.Default.GetBytes(str); //gb2312编码 汉字占2个字节、英文字母占1个字节 bytes长度为12
string s = System.Text.Encoding.Default.GetString(new byte[] { bytes[0],bytes[1] });//解码后为“中”
byte[] bytes = {97, 98, 99, 100, 101, 102};
string str = System.Text.Encoding.ASCII.GetString(bytes); //结果为:abcdef ASCII码表

2.比特数组=>字符串

(1)string str=System.Text.Encoding.Default.GetString(bt);

(2)string str=Convert.ToBase64String(bt);

3.字符串=>

(1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes("字符串"));

(2)MemoryStream ms=new MemoryStream(Convert.FromBase64String("字符串"));

4.流=>字符串

(1)string str=Convert.ToBase64String(ms.ToArray());

(2)string str=System.Text.Encoding.Default.GetString(ms.ToArray());

5.比特数组=>

(1)MemoryStream ms=new MemoryStream(bt);

(2)MemoryStream ms=new MemoryStream();ms.Read(bt,0,bt.Lenght);

6.流=>比特数组

(1)byte[] bt=ms.ToArray();

(2)MemoryStream ms=new MemoryStream();ms.Write(bt,0,ms.Length);

7、byte[]与base64string的互相转换

在C#中      

   图片到byte[]再到base64string的转换:

               Bitmap bmp = new Bitmap(filepath);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] arr = new byte[ms.Length];
ms.Position = ;
ms.Read(arr, , (int)ms.Length);
ms.Close();
string pic = Convert.ToBase64String(arr); base64string到byte[]再到图片的转换: byte[] imageBytes = Convert.FromBase64String(pic);
//读入MemoryStream对象
MemoryStream memoryStream = new MemoryStream(imageBytes, , imageBytes.Length);
memoryStream.Write(imageBytes, , imageBytes.Length);
//转成图片
Image image = Image.FromStream(memoryStream); 现在的数据库开发中:图片的存放方式一般有CLOB:存放base64string BLOB:存放byte[] 一般推荐使用byte[]。因为图片可以直接转换为byte[]存放到数据库中 若使用base64string 还需要从byte[]转换成base64string 。更浪费性能。

8、C# byte数组与Image的相互转换

http://www.cnblogs.com/luxiaoxun/p/3378416.html

C# string byte[] Base64 常用互相转换的更多相关文章

  1. C#string byte[] base64位互相转换

    byte表示字节,byte[]则表示存放一系列字节的数组 1个字符=2个字节(byte) 1个字节=8个比特(bit) 网速上所说的1M其实是指1兆的小b,1M= 1024b/8 = 128kb 下面 ...

  2. Byte[]和BASE64之间的转换

    一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...

  3. Base64与Bitmap转换

    Base64与Bitmap互转 /** * 将base64转为bitmap * * @param string * @return */ public Bitmap stringtoBitmap(St ...

  4. String类中常用的操作

    一.获取: 1.获取字符串的长度(注意是方法,不是跟数组的属性一样的) int length(); 1 public static void getLength(){ 2 String s = &qu ...

  5. MODBUS协议解析中常用的转换帮助类(C#)

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  6. guid与Base64编码互相转换

    guid的长度比较长,本文提供一种方法,将guid转为base64字符串,只有22位长度,比较好! 参考:https://blog.csdn.net/tgghfbflishuai/article/de ...

  7. 用java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)解决乱码问题

    Java中String的数据是如何存储的,查看源代码就可以知道,String的数据是存储在char[] value这样一个成员变量中的,char类型的大小在java中是2个字节 我们还知道,现在普遍使 ...

  8. 关于C# byte[]与struct的转换

    转自:http://blog.chinaunix.net/uid-215617-id-2213082.html Some of the C# code I've been writing recent ...

  9. String对象中常用的方法

    String对象中常用的方法   1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...

随机推荐

  1. 洛谷P2149 Elaxia的路线

    传送门啦 分析: 我最开始想的是跑两遍最短路,然后记录一下最短路走了哪些边(如果有两条最短路就选经过边多的),打上标记.两边之后找两次都标记的边有多少就行了. 但...我并没有实现出来. 最后让我们看 ...

  2. 在SQL中有时候我们需要查看现在正在SQL Server执行的命令

    在SQL中有时候我们需要查看现在正在SQL Server执行的命令.在分析管理器或者Microsoft SQL Server Management Studio中,我们可以在"管理-SQL  ...

  3. MySQL学习笔记:生成一个时间序列

    今天遇到一个需求是生成以下表格的数据,一整天24小时,每秒一行数据. 寻找颇旧,找到另外两个实现的例子,暂且学习一翻.另一个见另外一篇. DAY) AS DATE FROM ( ) AS tmp, ( ...

  4. 涨姿势系列之——内核环境下花式获得CSRSS进程id

    这个是翻别人的代码时看到的,所以叫涨姿势系列.作者写了一个获取CSRSS进程PID的函数,结果我看了好久才看懂是这么一个作用.先放上代码 HANDLE GetCsrPid() { HANDLE Pro ...

  5. java中举例说明对象调用静态成员变量

    package org.hanqi.zwxx; public class Test { static int i=47; public void call() { System.out.println ...

  6. MySQL 相关知识细节及解析

    1,删除表中所有记录使用delete from 表名:还是用truncate table 表名 删除方式:delete 一条一条删除,不清空auto_increment记录数 truncate 直接将 ...

  7. 纯CSS仿制Google女生节Doodle

    看到google今天的女生节Doodle,自己用纯css仿制一个,送给老妈.老婆.女儿. 大家可以点这里在线观看效果,点这里下载收藏效果. 实现原理 1.利用checkbox侦听处理单击事件. 2.单 ...

  8. react篇章-React Props-Props 验证

    React.PropTypes 在 React v15.5 版本后已经移到了 prop-types 库. <script src="https://cdn.bootcss.com/pr ...

  9. vue 组件使用中的细节点

    1.is属性 有些 HTML 元素,诸如 <ul>.<ol>.<table> 和 <select>,对于哪些元素可以出现在其内部是有严格限制的.而有些元 ...

  10. CodeForces - 600C Make Palindrome 贪心

    A string is called palindrome if it reads the same from left to right and from right to left. For ex ...