参考:

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. 洛谷P2886牛继电器

    传送门啦 倍增 $ Floyd $ 注意结构体里二维数组不能开到 $ 2000 $ #include <iostream> #include <cstdio> #include ...

  2. iframe框架加载完成后执行函数

    var iframe = document.createElement("iframe"); iframe.src = "http://www.baidu.com/&qu ...

  3. SQL语句资料

    --语 句 功 能  --数据操作  SELECT --从数据库表中检索数据行和列  INSERT --向数据库表添加新数据行  DELETE --从数据库表中删除数据行  UPDATE --更新数据 ...

  4. KDE 下 U 盘挂载失败

    重装完 KDE 后发现在 dolphin 中无法挂载 U 盘了,提示 unable to authenticate.但是用 udisksctl 却可以挂载: yyc@TDesk run > ud ...

  5. Rookey.Frame之DAL工厂

    昨天给大家介绍了表单验证功能,今天给大家介绍下Rookey.Frame框架的数据层工厂,由于Rookey.Frame框架ORM是基于servicestack.ormlite,很多朋友反映这个网上中文资 ...

  6. 三 oracle 用户管理一

    一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码; ...

  7. Hadoop整理二(Hadoop分布式存储系统HDFS)

    一.背景 当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区(partition) 并存储到若干台单独的计算机上.管理网络中跨多台计算机存储的文件系统称为分布式文件系统 (dist ...

  8. Bzoj4548 小奇的糖果(链表+树状数组)

    题面 Bzoj 题解 很显然,我们只需要考虑单独取线段上方的情况,对于下方的把坐标取反再做一遍即可(因为我们只关心最终的答案) 建立树状数组维护一个横坐标区间内有多少个点,维护双向链表实现查询一个点左 ...

  9. java-Excel导出中的坑

    在Excel导出过程中,若遇到合并单元格样式只有第一行合并,而下面要合并的行没有边框显示. 一般问题出在将单元格样式设置与合并单元格放在同一个循环中导致. 以下为一个完整版的demo以供参考 定义边框 ...

  10. [flask]flask_login模块,session及其他

    读flask源码的时候,有一点一直到现在都没有一个清晰的概念,比如四个全局变量g,current_app,session,request是怎么做到的 按照查到的资料里面的说法,为了不至于每次都主动调用 ...