.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
C#中string与byte[]的转换帮助类

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    在写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>
 /// 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
 }
}




相关文章

相关软件