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

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

更新时间:2008年03月09日 20:19:08   作者:

在写c#程序时,string和byte[]之间的转换比较烦,在移植一些老程序时感觉很不好。我在c#中使用des和tripledes时移植一块老代码时也遇到了同样的情况。为了下次不为同样的事情烦恼,就写了下面的帮助类。

主要实现了以下的函数

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

/**//*

* @author wuerping

* @version 1.0

* @date 2004/11/30

* @description:

*/

using system;

using system.text;

namespace sidlehelper

{

/** 

/// summary description for strhelper.

/// 命名缩写:

/// str: unicode string

/// arr: unicode array

/// hex: 二进制数据

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

/// asc: ascii

/// uni: unicode

/// 

public sealed class strhelper

{

hex与hexbin的转换#region hex与hexbin的转换

public static void hexbin2hex(byte[] bhexbin, byte[] bhex, int nlen)

{

for(int i=0; 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);

}

}

}

public static byte[] hexbin2hex(byte[] bhexbin, int nlen)

{

if(nlen%2 !=0)

return null;

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

hexbin2hex(bhexbin, bhex, nlen);

return bhex;

}

public static void hex2hexbin(byte[] bhex, byte[] bhexbin, int nlen)

{

byte c;

for(int i=0;i

{

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

if(c 

{

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

}

else

{

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

}

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

if(c 

{

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

}

else

{

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

}

}

}

public static byte[] hex2hexbin(byte[] bhex, int nlen)

{

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

hex2hexbin(bhex, bhexbin, nlen);

return bhexbin;

}

#endregion

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

public static byte[] str2arr(string s)

{

return (new unicodeencoding()).getbytes(s);

}

public static string arr2str(byte[] buffer)

{

return (new unicodeencoding()).getstring(buffer, 0, buffer.length);

}

public static byte[] str2ascarr(string s)

{

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

system.text.encoding.ascii,

str2arr(s));

}

public static byte[] str2hexascarr(string s)

{

byte[] hex = str2ascarr(s);

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

return hexbin;

}

public static string ascarr2str(byte[] b)

{

return system.text.unicodeencoding.unicode.getstring(

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

system.text.encoding.unicode,

b)

);

}

public static string hexascarr2str(byte[] buffer)

{

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

return ascarr2str(b);

}

#endregion

}

}

相关文章

1a1b05c64693fbf380aa1344a7812747.png

本文主要介绍了webUploader上传大视频文件相关web.config的配置。具有一定的参考价值,下面跟着小编一起来看下吧2017-01-01

4f55910a645b073bc4fc65dc10dc14bd.png

这篇文章主要介绍了设计windows phone页面主题,需要的朋友可以参考下2015-07-07

0ea3c7666119d5615e582f823fb3fad6.png

在使用多线程进行编程时,有一些经典的线程同步问题,对于这些问题,.net提供了多种不同的类来解决2012-11-11

4f96a78db829b1556ff16de21e013c7a.png

ADO.NET实用技巧两则...2006-07-07

8cc1031babc6aff2319f1c6af8544aa0.png

这篇文章主要介绍了upload上传单张图片的代码,需要的朋友可以参考下。2015-07-07

0c932a99bb7b6f23c937db507070cc7b.png

Oracle中TO_DATE格式介绍;可供需求的朋友参考2012-11-11

cca732bf65a93ed2ec0ac80c638460fe.png

学习是使用asp.net已经有很长一段时间了,现在就来分析一下mvc的整过过程吧。个人计划写一个mvc系列的博文,仅从源代码的角度来分析mvc。在接触mvc时我们一定会经历路由,那么路由这东东是怎么搞出来的啊2012-11-11

2d9f31f2af7b675a3d153d2b7f1035a7.png

本篇文章是对XmlSerializer 对象的Xml序列化与反序列化的应用进行了详细的分析介绍,需要的朋友参考下2013-05-05

b452cee8ec5cd9e58ab98eba17281e59.png

现在我们转入客户端,并添加一个能够使用从Admin控制器而来的数据的页面。通过给控制器发送AJAX请求的方式,该页面将允许用户创建、编辑,或删除产品2012-11-11

f4838ec7e2d4da28e0b57d4e852dadd4.png

ASP.NET入门数据篇...2006-07-07

最新评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值