C#中string与byte[]的转换
2008-10-07 11:48
295 查看
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
}
}
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#中文件转换为byte[]及Base64String
- c#中byte[]和string的转换
- c#byte[]和string,Image;Stream 和 文件之间的转换
- C# string类型和byte[]类型相互转换
- c#中 uint--byte[]--char[]--string相互转换汇总
- C# byte数组转换string类型如何去掉结束符
- C# 图片与byte[]之间以及byte[]与string之间的转换
- C#中byte[]与string的转换
- string和byte[]之间的转换 (C#)
- c# byte char string转换
- C#中有关string和byte[]转换的问题
- c#中 uint--byte[]--char[]--string相互转换汇总
- c#中 uint--byte[]--char[]--string相互转换汇总
- c#中byte[]和string的转换
- string和byte[]的转换 (C#)
- c#中 uint--byte[]--char[]--string相互转换汇总
- C# string byte数组转换解析
- C#中有关string和byte[]转换的问题
- [收藏]C#中string与byte[]的转换帮助类-.NET教程,C#语言
- C# 中byte[] 和string类型的转换