asp.net 字符串、二进制、编码数组转换函数

时间:2024-03-10 07:43:23 

1.字符串转二进制数组
string content="这是做个测试!";

System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);

2.二进制数组转为字符串


System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
string spcontent = converter.GetString(byteArr );



在编程中会遇到将文件以二进制数据保存到数据库的情况,以将"C:\test.html"文件保存到数据库和读取出来为例:

1.将文件以流的形式保存到数据库中:


int itag=0;

string content = "";

StringBuilder sb = new StringBuilder();

string fileName = @"c:\test.html";
StreamReader objReader = new StreamReader(fileName, System.Text.Encoding.GetEncoding("gb2312"));
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{//这里可以做相应的处理,如过滤文件中的数据
sb.Append(sLine);

}
}

objReader.Close();

content= sb.ToString(); //如果你要将整个文件的内容显示在界面上,你可以用<%=content%>放到相应的地方

System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] byteArr = converter.GetBytes(content);

//下面为插入到数据库代码,

strInsertCmd = "insert into Document (DocumentID,DocumentContent,addtime,MODITIME,status) values ('" + DocumentID + "',?,'" + NOWTIME + "','" + NOWTIME + "',' 00 ')";
cmd=new OleDbCommand(strInsertCm,ConnectionOBJ);
param = new OleDbParameter("DocumentContent", OleDbType.VarBinary, byteArr.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, byteArr);
cmd.Parameters.Add(param);
itag=cmd.ExecuteNonQuery();

if(itag>0){//成功!}



2.从数据库中读取保存为文件或者字符串和步骤1是一个相反的过程

1.将GB2312数据转换为UTF-8数据如下(其他的编码类推):


public string GB2312ToUTF8(string sSourse) {
string Utf8_info = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
byte[] unicodeBytes = gb2312.GetBytes(sSourse);
byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);
char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
Utf8_info = new string(asciiChars);
return Utf8_info;
}
标签:asp.net,字符串,二进制,编码
0
投稿

猜你喜欢

  • 不完全HTML在线编辑器收集

    2007-11-08 12:20:00
  • Python+OpenCV采集本地摄像头的视频

    2021-12-06 23:11:31
  • 很全面的MySQL处理重复数据代码

    2024-01-24 14:26:45
  • 基于Python实现快递信息提取

    2022-05-02 13:41:11
  • pycharm 如何跳出服务器证书不受信任的提示

    2022-01-15 16:38:46
  • Python模块汇总(常用第三方库)

    2023-05-21 16:25:37
  • python3 mmh3安装及使用方法

    2021-08-02 00:31:05
  • 解决jupyter (python3) 读取文件遇到的问题

    2021-02-05 10:00:25
  • python virtualenv虚拟环境配置与使用教程详解

    2023-01-21 23:06:56
  • SQL 2008 FileStream数据类型

    2008-10-28 21:07:00
  • 关于javascript原型的修改与重写(覆盖)差别详解

    2023-07-02 05:07:26
  • Python 创建空的list,以及append用法讲解

    2021-02-23 17:05:58
  • Python字典取键、值对的方法步骤

    2021-12-12 15:26:12
  • Python实现名片管理系统

    2022-01-11 21:45:12
  • SQL数据类型详解

    2024-01-13 02:01:30
  • Vue组件之自定义事件的功能图解

    2024-05-05 09:07:34
  • 对SQL Server聚集索引的指示综合描述

    2010-08-31 14:25:00
  • MySQL创建、修改和删除表操作指南

    2024-01-19 20:32:59
  • vue实现导航栏效果(选中状态刷新不消失)

    2024-05-09 15:18:31
  • 50个常用sql语句 网上流行的学生选课表的例子

    2012-07-11 16:02:01
  • asp之家 网络编程 m.aspxhome.com