手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >C#中string与byte[]的转换帮助类-.NET教程,C#语言
C#中string与byte[]的转换帮助类-.NET教程,C#语言
摘要:主要实现了以下的函数代码中出现的sidle是我的网名。/**//**@authorwuerping*@version1.0*@date200...

主要实现了以下的函数

代码中出现的sidle是我的网名。

/**//*

*@authorwuerping

*@version1.0

*@date2004/11/30

*@description:

*/

usingsystem;

usingsystem.text;

namespacesidlehelper

{

/**////<summary>

///summarydescriptionforstrhelper.

///命名缩写:

///str:unicodestring

///arr:unicodearray

///hex:二进制数据

///hexbin:二进制数据用ascii字符表示例字符1的hex是0x31表示为hexbin是31

///asc:ascii

///uni:unicode

///</summary>

publicsealedclassstrhelper

{

hex与hexbin的转换#regionhex与hexbin的转换

publicstaticvoidhexbin2hex(byte[]bhexbin,byte[]bhex,intnlen)

{

for(inti=0;i<nlen/2;i++)

{

if(bhexbin[2*i]<0x41)

{

bhex[i]=convert.tobyte(((bhexbin[2*i]-0x30)<<4)&0xf0);

}

else

{

bhex[i]=convert.tobyte(((bhexbin[2*i]-0x37)<<4)&0xf0);

}

if(bhexbin[2*i+1]<0x41)

{

bhex[i]|=convert.tobyte((bhexbin[2*i+1]-0x30)&0x0f);

}

else

{

bhex[i]|=convert.tobyte((bhexbin[2*i+1]-0x37)&0x0f);

}

}

}

publicstaticbyte[]hexbin2hex(byte[]bhexbin,intnlen)

{

if(nlen%2!=0)

returnnull;

byte[]bhex=newbyte[nlen/2];

hexbin2hex(bhexbin,bhex,nlen);

returnbhex;

}

publicstaticvoidhex2hexbin(byte[]bhex,byte[]bhexbin,intnlen)

{

bytec;

for(inti=0;i<nlen;i++)

{

c=convert.tobyte((bhex[i]>>4)&0x0f);

if(c<0x0a)

{

bhexbin[2*i]=convert.tobyte(c+0x30);

}

else

{

bhexbin[2*i]=convert.tobyte(c+0x37);

}

c=convert.tobyte(bhex[i]&0x0f);

if(c<0x0a)

{

bhexbin[2*i+1]=convert.tobyte(c+0x30);

}

else

{

bhexbin[2*i+1]=convert.tobyte(c+0x37);

}

}

}

publicstaticbyte[]hex2hexbin(byte[]bhex,intnlen)

{

byte[]bhexbin=newbyte[nlen*2];

hex2hexbin(bhex,bhexbin,nlen);

returnbhexbin;

}

#endregion

数组和字符串之间的转化#region数组和字符串之间的转化

publicstaticbyte[]str2arr(strings)

{

return(newunicodeencoding()).getbytes(s);

}

publicstaticstringarr2str(byte[]buffer)

{

return(newunicodeencoding()).getstring(buffer,0,buffer.length);

}

publicstaticbyte[]str2ascarr(strings)

{

returnsystem.text.unicodeencoding.convert(system.text.encoding.unicode,

system.text.encoding.ascii,

str2arr(s));

}

publicstaticbyte[]str2hexascarr(strings)

{

byte[]hex=str2ascarr(s);

byte[]hexbin=hex2hexbin(hex,hex.length);

returnhexbin;

}

publicstaticstringascarr2str(byte[]b)

{

returnsystem.text.unicodeencoding.unicode.getstring(

system.text.asciiencoding.convert(system.text.encoding.ascii,

system.text.encoding.unicode,

b)

);

}

publicstaticstringhexascarr2str(byte[]buffer)

{

byte[]b=hex2hexbin(buffer,buffer.length);

returnascarr2str(b);

}

#endregion

}

}

【C#中string与byte[]的转换帮助类-.NET教程,C#语言】相关文章:

asp.net+js实现的ajax sugguest搜索提示效果

asp.net 序列化and反序列化演示

ASP.NET中Global和URLReWrite用法

asp.net(c#)两种随机数的算法,可用抽考题

asp.net使用jquery模板引擎jtemplates呈现表格

ASP.NET Ref和Out关键字区别分析

将文本文件的内容或者文字保存成图片的方法介绍

用存储过程向数据库存值的具体实现

C#中的委托和事件学习(续)

ASP.Net中数据展示控件的嵌套使用示例

精品推荐
分类导航