c#⼆进制、⼗六进制与字节数组的相互转换在c#项⽬中⽤到的⼆进制、⼗六进制与字节数组的相互转换
class HexCommon
{
/// <summary>
/// 16进制字符串转字节数组
/// 如01 02 ff 0a
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[]strHexToByte(string hexString)
{
hexString = hexString.Replace(" ","");
if((hexString.Length %2)!=0)
hexString +=" ";
byte[] returnBytes =new byte[hexString.Length /2];
for(int i =0; i < returnBytes.Length; i++)
{
string temp = hexString.Substring(i *2,2).Trim();
returnBytes[i]= Convert.ToByte(temp,16);
}
return returnBytes;
}
/
// <summary>
/// 6进制字符串转字节数组
/// </summary>
/// <param name="hexString"></param>
/// <param name="separator">分隔符</param>
/// <returns></returns>
public static byte[]strHexToByte(string hexString,char separator)
{
if(separator ==char.MinValue)
{
return strHexToByte(hexString);
}
string[] hexarray = hexString.Split(separator);
param nameList<string> temphexarray =new List<string>(hexarray);
var newhexarray = temphexarray.Where(u => u !=string.Empty);
int len = newhexarray.Count();
byte[] returnBytes =new byte[len];
int i =0;
foreach(string str in newhexarray)
{
string temp = str.Trim();
returnBytes[i]= Convert.ToByte(temp,16);
i = i +1;
}
return returnBytes;
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr ="";
if(bytes !=null)
{
for(int i =0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("x2")+" ";
}
}
return returnStr.Trim();
}
/
// <summary>
/// 字节数组转16进制字符串 (⾼低位反)
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStrHL(byte[] bytes)
{
string returnStr ="";
try
{
if(bytes !=null)
{
for(int i =1; i < bytes.Length;)
{
returnStr += bytes[i].ToString("x2")+" ";
returnStr += bytes[i -1].ToString("x2")+" ";
i = i +2;
}
}
}
catch(Exception ex)
{
}
return returnStr.Trim();
}
/// <summary>
/// ⼆进制字符串转字节数组
/// 如01001111 011111111
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[]strBinaryToByte(string binaryString)
{
binaryString = binaryString.Replace(" ","");
int yu = binaryString.Length %8;
//将字符串长度变成8的倍数
if(yu !=0)
binaryString = binaryString.PadRight(binaryString.Length +8- yu); StringBuilder builder =new StringBuilder();
byte[] returnBytes =new byte[binaryString.Length /8];
for(int i =0; i < returnBytes.Length; i++)
{
string temp = binaryString.Substring(i *8,8).Trim();
returnBytes[i]= Convert.ToByte(temp,2);
}
return returnBytes;
}
/// <summary>
/// <summary>
/// ⼆进制字符串转字节数组
/// 如01001111 011111111
/// </summary>
/// <param name="hexString"></param>
/// <param name="separator">分隔符</param>
/// <returns></returns>
public static byte[]strBinaryToByte(string binaryString,char separator) {
if(separator ==char.MinValue)
{
return strBinaryToByte(binaryString);
}
string[] binarray = binaryString.Split(separator);
List<string> tempbinarray =new List<string>(binarray);
var newbinarray = tempbinarray.Where(u => u !=string.Empty);
int len = newbinarray.Count();
byte[] returnBytes =new byte[len];
int i =0;
foreach(string str in newbinarray)
{
string temp = str.Trim();
returnBytes[i]= Convert.ToByte(temp,2);
i = i +1;
}
return returnBytes;
}
/// <summary>
/// ⼆进制字符串转16进制字符串(⽆分隔符)
/// </summary>
/// <param name="binaryString"></param>
/
// <returns></returns>
public static string strBinaryTostrHex(string binaryString)
{
binaryString = binaryString.Replace(" ","");
int yu = binaryString.Length %8;
//将字符串长度变成8的倍数
if(yu !=0)
binaryString = binaryString.PadRight(binaryString.Length +8- yu); StringBuilder builder =new StringBuilder();
int len = binaryString.Length /8;
for(int i =0; i < len; i++)
{
string temp = binaryString.Substring(i *8,8).Trim();
string str = Convert.ToInt32(temp,2).ToString("x2");
builder = builder.Append(str);
}
return builder.ToString();
}
/// <summary>
/// ⼆进制字符串转16进制字符串(有分隔符)
/// </summary>
/// <param name="binaryString"></param>
/// <param name="separator">分隔符</param>
/
// <returns></returns>
public static string strBinaryTostrHex(string binaryString,char separator) {
if(separator ==char.MinValue)
{
return strBinaryTostrHex(binaryString);
}
string[] binarray = binaryString.Split(separator);
string[] binarray = binaryString.Split(separator);
StringBuilder builder =new StringBuilder();
int len = binarray.Length;
for(int i =0; i < len; i++)
{
string bin = binarray[i].Trim();
if(bin ==string.Empty)continue;
string str = Convert.ToInt32(bin,2).ToString("x2");
builder = builder.Append(str);
builder = builder.Append(separator);
}
string returnvalue = builder.ToString();
returnvalue = returnvalue.TrimEnd(separator);
return returnvalue;
}
/
// <summary>
/// 16进制字符串转换成2进制字符串,此⽅法⽤于没有分隔符
/// 16进制格式"ff 01" 2进制格式 "10101001 11111111"
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static string strHexTostrBinary(string hexString)
{
hexString = hexString.Replace(" ","");
if((hexString.Length %2)!=0)
{
hexString = hexString.PadRight(hexString.Length +1);
}
StringBuilder builder =new StringBuilder();
int len = hexString.Length /2;
for(int i =0; i < len; i++)
{
string hex = hexString.Substring(i *2,2).Trim();
int a = Convert.ToInt32(hex,16);
string str = Convert.ToString(a,2).PadLeft(8,'0');
builder = builder.Append(str);
}
return builder.ToString();
}
/// <summary>
/// 16进制字符串转换成2进制字符串,此⽅法⽤于有分隔符如空格
/// 16进制格式"ff 01" 2进制格式 "10101001 11111111"
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static string strHexTostrBinary(string hexString,char separator) {
if(separator ==char.MinValue)
return strHexTostrBinary(hexString);
string[] hexarray = hexString.Split(separator);
StringBuilder builder =new StringBuilder();
int len = hexarray.Length;
for(int i =0; i < len; i++)
{
string hex = hexarray[i].Trim();
if(hex ==string.Empty)continue;
int a = Convert.ToInt32(hex,16);
string str = Convert.ToString(a,2).PadLeft(8,'0');
string str = Convert.ToString(a,2).PadLeft(8,'0'); builder = builder.Append(str);
builder = builder.Append(separator.ToString());
}
string returnvalue = builder.ToString();
returnvalue = returnvalue.TrimEnd(separator);
return returnvalue;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论