[收藏]C#中string与byte[]的转换帮助类-.NET教程,C#语言

None.gif 在写c#程序时,string和byte[]之间的转换比较烦,在移植一些老程序时感觉很不好。我在c#中使用des和tripledes时移植一块老代码时也遇到了同样的情况。为了下次不为同样的事情烦恼,就写了下面的帮助类。 
None.gif主要实现了以下的函数 
None.gif
None.gif
None.gif代码中出现的sidle是我的网名。 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* @author wuerping 
InBlock.gif* @version 1.0 
InBlock.gif* @date 2004/11/30 
InBlock.gif* @description: 
ExpandedBlockEnd.gif
*/
 
None.gif
using  system; 
None.gif
using  system.text; 
None.gif
namespace  sidlehelper 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
/// summary description for strhelper. 
InBlock.gif
/// 命名缩写: 
InBlock.gif
/// str: unicode string 
InBlock.gif
/// arr: unicode array 
InBlock.gif
/// hex: 二进制数据 
InBlock.gif
/// hexbin: 二进制数据用ascii字符表示 例 字符1的hex是0x31表示为hexbin是 31 
InBlock.gif
/// asc: ascii 
InBlock.gif
/// uni: unicode 
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gifpublic sealed class strhelper 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
hex与hexbin的转换#region hex与hexbin的转换 
InBlock.gif
public static void hexbin2hex(byte[] bhexbin, byte[] bhex, int nlen) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
for(int i=0; i<nlen/2; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if(bhexbin[2*i] <0x41
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhex[i] 
= convert.tobyte(((bhexbin[2*i] - 0x30)<<4& 0xf0); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhex[i] 
= convert.tobyte(((bhexbin[2*i] - 0x37)<<4& 0xf0); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
if(bhexbin[2*i+1<0x41
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhex[i] 
|= convert.tobyte((bhexbin[2*i+1- 0x30& 0x0f); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhex[i] 
|= convert.tobyte((bhexbin[2*i+1- 0x37& 0x0f); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public static byte[] hexbin2hex(byte[] bhexbin, int nlen) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
if(nlen%2 !=0
InBlock.gif
return null
InBlock.gif
byte[] bhex = new byte[nlen/2]; 
InBlock.gifhexbin2hex(bhexbin, bhex, nlen); 
InBlock.gif
return bhex; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public static void hex2hexbin(byte[] bhex, byte[] bhexbin, int nlen) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
byte c; 
InBlock.gif
for(int i=0;i<nlen;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
= convert.tobyte((bhex[i]>>4& 0x0f); 
InBlock.gif
if(c < 0x0a
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhexbin[
2*i] = convert.tobyte(c + 0x30); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhexbin[
2*i] = convert.tobyte(c + 0x37); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
= convert.tobyte(bhex[i]&0x0f); 
InBlock.gif
if(c < 0x0a
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhexbin[
2*i+1= convert.tobyte(c + 0x30); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gifbhexbin[
2*i+1= convert.tobyte(c + 0x37); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public static byte[] hex2hexbin(byte[] bhex, int nlen) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
byte[] bhexbin = new byte[nlen*2]; 
InBlock.gifhex2hexbin(bhex, bhexbin, nlen); 
InBlock.gif
return bhexbin; 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif
#endregion
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
数组和字符串之间的转化#region 数组和字符串之间的转化 
InBlock.gif
public static byte[] str2arr(string s) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return (new unicodeencoding()).getbytes(s); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public static string arr2str(byte[] buffer) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return (new unicodeencoding()).getstring(buffer, 0, buffer.length); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public static byte[] str2ascarr(string s) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return system.text.unicodeencoding.convert(system.text.encoding.unicode, 
InBlock.gifsystem.text.encoding.ascii, 
InBlock.gifstr2arr(s)); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public static byte[] str2hexascarr(string s) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
byte[] hex = str2ascarr(s); 
InBlock.gif
byte[] hexbin = hex2hexbin(hex, hex.length); 
InBlock.gif
return hexbin; 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
public static string ascarr2str(byte[] b) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
return system.text.unicodeencoding.unicode.getstring( 
InBlock.gifsystem.text.asciiencoding.convert(system.text.encoding.ascii, 
InBlock.gifsystem.text.encoding.unicode, 
InBlock.gifb) 
InBlock.gif); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
public static string hexascarr2str(byte[] buffer) 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
byte[] b = hex2hexbin(buffer, buffer.length); 
InBlock.gif
return ascarr2str(b); 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockEnd.gif
#endregion
 
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}
 
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值