C#中string与byte[]的转换帮助类-.NET教程,C#语言_asp.net教程-查字典教程网
C#中string与byte[]的转换帮助类-.NET教程,C#语言
C#中string与byte[]的转换帮助类-.NET教程,C#语言
发布时间:2016-12-29 来源:查字典编辑
摘要:主要实现了以下的函数代码中出现的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

}

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新asp.net教程学习
    热门asp.net教程学习
    编程开发子分类