加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

C#中string与byte[]的切换帮助类

发布时间:2023-04-11 14:02:28 所属栏目:语言 来源:
导读:主要实现了以下的函数

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

/**//*

* @author wuerping

* @version 1.0

* @date 2004/11/30

* @description:

*/

using system;

using system.text;
主要实现了以下的函数
 
代码中出现的sidle是我的网名。
 
/**//*
 
* @author wuerping
 
* @version 1.0
 
* @date 2004/11/30
 
* @description:
 
*/
 
using system;
 
using system.text;
 
namespace sidlehelper
 
{
 
/**//// <summary>
 
/// summary description for strhelper.
 
/// 命名缩写:
 
/// str: unicode string
 
/// arr: unicode array
 
/// hex: 二进制数据
 
/// hexbin: 二进制数据用ascii字符表示 例 字符1的hex是0x31表示为hexbin是 31
 
/// asc: ascii
 
/// uni: unicode
 
/// </summary>
 
public sealed class strhelper
 
{
 
hex与hexbin的转换#region hex与hexbin的转换
 
public static void hexbin2hex(byte[] bhexbin, byte[] bhex, int nlen)
 
{
 
for(int i=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);
 
}
 
}
 
}
 
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<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);
 
}
 
}
 
}
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!