using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BaseLibRWFile
{
public class DataFormatConversion
{
#region ByteToString
///
/// 字节数组到十六进制字符串
///
/// 待转换字节数组
/// 十六进制字符串
public static string ByteToHexStr(byte[] bytes)//byte数组转换为十六进制字符串
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");//ToString("X2") 为C#中的字符串格式控制符
}
}
return returnStr;
}
///
/// Bytes to string.
///
/// The data.
/// System.String.
public static string ByteToString(byte[] data)
{
return Encoding.Default.GetString(data);
}
//字节变量返回8为二进制字符串
///
/// 字节变量返回8位二进制字符串
///
/// 字节变量值
///
public string ByteToBinary(byte bValue)
{
string strTemp;
int i, strLen;
strTemp = Convert.ToString(bValue, 2);
if (strTemp.Length < 8)
{
strLen = strTemp.Length;
for (i = 0; i < 8 - strLen; i++)
{
strTemp = "0" + strTemp;
}
}
return strTemp;
}
#endregion
///
/// 字符串转换为16进制
///
///
///
public static string StringToHexString(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
s.Append(c.ToString("X4"));
}
return s.ToString();
}
///
/// 字符串转16进制字节数组
///
/// 待转换字符串
/// 字节数组
public static byte[] HexStrTobyte(string hexString)//十六进制字符串转换为byte数组
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
return returnBytes;
}
///
/// Strings to byte.
///
/// The value.
/// System.Byte[].
public static byte[] StringToByte(string value)
{
return Encoding.Default.GetBytes(value);
}
///
/// 字符串转换16进制字节数组
///
///
///
public static byte[] StrToHexByte(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++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ", ""), 16);
return returnBytes;
}
public static string StrToHexBytes(string hexString)
{
hexString = hexString.Trim();
byte[] b = Encoding.ASCII.GetBytes(hexString);//按照指定编码将string编程字节数组
string[] strA = new string[b.Length];
string strB = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
{
strA[i] = Convert.ToString(b[i], 16);
}
int il = (int)Math.Ceiling((double)b.Length / 2);
for (int i = 0; i < il; i++)
{
if (i * 2 + 1 > strA.Length - 1)
{
strB += "00";
}
else
{
strB += strA[i * 2 + 1];
}
strB += strA[i * 2];
}
return strB;
}
public static string HexStringToBinary(string[] str)
{
byte[] buff = new byte[str.Length * 2];
for (int i = 0; i < str.Length; i++)
{
buff[i * 2] = Convert.ToByte(str[i].Substring(2, 2), 16);
buff[i * 2 + 1] = Convert.ToByte(str[i].Substring(0, 2), 16);
}
string result = Encoding.Default.GetString(buff);
result = result.Trim();
result = result.TrimEnd('\0');
result = result.TrimStart('\0');
return result;
}
}
}