asp.net 字符串、二进制、编码数组转换函数
程序员文章站
2024-03-08 12:35:16
1.字符串转二进制数组 string content="这是做个测试!"; system.text.unicodeencoding converter = new syst...
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;
}
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;
}
上一篇: 详解SpringMVC中设置静态资源不被拦截的问题
下一篇: Arrays.asList方法总结