C#中string与byte[]的转换帮助类
生活随笔
收集整理的這篇文章主要介紹了
C#中string与byte[]的转换帮助类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在寫C#程序時,string和byte[]之間的轉(zhuǎn)換比較煩,在移植一些老程序時感覺很不好。我在C#中使用DES和TripleDES時移植一塊老代碼時也遇到了同樣的情況。為了下次不為同樣的事情煩惱,就寫了下面的幫助類。
主要實現(xiàn)了以下的函數(shù)
代碼中出現(xiàn)的Sidle是我的網(wǎng)名。
/*
* @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: 二進制數(shù)據(jù)
/// Hexbin: 二進制數(shù)據(jù)用ASCII字符表示 例 字符'1'的hex是0x31表示為hexbin是 '3''1'
/// Asc: ASCII
/// Uni: UNICODE
/// </summary>
public sealed class StrHelper
{
#region Hex與Hexbin的轉(zhuǎn)換
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 數(shù)組和字符串之間的轉(zhuǎn)化
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
}
}?
?
主要實現(xiàn)了以下的函數(shù)
代碼中出現(xiàn)的Sidle是我的網(wǎng)名。
/*
* @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: 二進制數(shù)據(jù)
/// Hexbin: 二進制數(shù)據(jù)用ASCII字符表示 例 字符'1'的hex是0x31表示為hexbin是 '3''1'
/// Asc: ASCII
/// Uni: UNICODE
/// </summary>
public sealed class StrHelper
{
#region Hex與Hexbin的轉(zhuǎn)換
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 數(shù)組和字符串之間的轉(zhuǎn)化
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
}
}?
?
總結(jié)
以上是生活随笔為你收集整理的C#中string与byte[]的转换帮助类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二手音响价格大概需要多少
- 下一篇: C#时间函数扩展