上一篇:使用C#在进度条中显示复制文件的进度 >>
C#中string与byte[]的转换帮助类
主要实现了以下的函数
代码中出现的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是 @#3@#@#1@#
/// Asc: ASCII
/// Uni: UNICODE
/// </summary>
public sealed class StrHelper
{
#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);
}
}
}
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
}
}
相关文章:
- · 【C#】Decimal的类型判断!
- · 用c#写的贪吃蛇游戏,简简单单
- · 天天学C#-委托
- · 天天学C#-委托
- · C#中的泛型 (From dotNet SDK 2.0 Beta1)
- · 在C#编写代码发送邮件
- · 使用VB求解华容道问题
- · C#使用CDO发送邮件
- · 读《C#入门经典》
- · 【C#】整数类型的判断!
- · c#下重写BUTTON控件
- · 怎样在C#中调用Delphi6写的DLL
- · C#向Sql Server中插入记录时单引号的处理
- · 使用 Visual C# .NET 检查 Windows 版本
- · C#:获得文件版本信息及只读文件的删除
- · C#:文件的按行读/写及文件目录对话框的使用
- · 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格
- · C#:ListBox的2个常用方法Add,Clear与Items的2个属性
- · 用C#实现WEB浏览器
- · C#读写注册表
- · C#数据库连接字符大全
- · C#编码规范.doc
- · 用C#给程序加启动画面并只允许一个应用程序实例运行
- · 使用C#在进度条中显示复制文件的进度
- · 使用C#编写Ice应用程序
- · C#多线程共享数据
- · C#开发小技巧--对Text控件增量赋值的代码优化
- · C#实现Singleton的两种方法的比较
- · C#下的webservcie 实现代码和 在vc和python下的调用实现
- · VB中常用的数据类型和C#中数据类型的对应
- · [ASP.net(C#)]XML操作类(一)
- · 发表一个自己C#写的email 初级系统源代码
- · c#的开发环境
- · C# 1.x 实现 强类型元素唯一的 ArrayList
- · c# encrypt
- · C++编程人员容易犯的10个C#错误
- · c#中结构与类的区别
- · C# 编码规范和编程好习惯
