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

简介:


* @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 

}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/articles/1499350.html,如需转载请自行联系原作者


相关文章
|
28天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
37 0
|
6天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
18天前
|
存储 算法 C语言
【C++初阶】8. STL初阶 + String类
【C++初阶】8. STL初阶 + String类
48 1
|
18天前
|
C语言 C++
【C++初阶】9. string类的模拟实现
【C++初阶】9. string类的模拟实现
38 1
|
1月前
|
存储 编译器 C++
string类的模拟实现
string类的模拟实现
29 0
|
1月前
|
编译器 Linux C++
string类的函数讲解
string类的函数讲解
15 1
|
1月前
|
存储 缓存 编译器
C++:String类的使用
C++:String类的使用
|
1月前
|
Java 索引
【Java】String类常用方法总结
【Java】String类常用方法总结
20 0